Edit specific XML data with LOOP using form in PHP file

Asked

Viewed 612 times

0

I’m having some problems trying to edit specific data (with LOOP) of XML with form in a PHP page.

PHP file: input.php (no loop)

<meta charset="UTF-8">
<?php
    if(isset($_POST['submit'])){
        $carregador_xml=simplexml_load_file('minha_lista.xml');
        $carregador_xml->lista->título=$_POST['título'];
        $função=fopen("minha_lista.xml","wb");
        fwrite($função,$carregador_xml->asXML());
        fclose($função);
    }
    $carregador_xml=simplexml_load_file('minha_lista.xml');
    $título=$carregador_xml->lista->título;
?>
<form id="yourFormId" method="post">
    <input name="título" value="<?php echo $título ?>">
    <input class="submitBtn" type="submit" name="submit" value="Enviar Registro!">
</form>

PHP file: input.php (with loop)

<meta charset="UTF-8">
<?php
    $carregador_xml = simplexml_load_file("minha_lista.xml");
    foreach($carregador_xml->children() as $lista){
?>
<form method="post">
    <input name="título" value="<?php echo $título ?>">
    <input class="submitBtn" type="submit" name="submit" value="Enviar Registro!">
</form>
<?php
    if(isset($_POST['submit'])){
        $carregador_xml=simplexml_load_file('minha_lista.xml');
        $carregador_xml->lista->título=$_POST['título'];
        $handle=fopen("minha_lista.xml","wb");
        fwrite($handle,$carregador_xml->asXML());
        fclose($handle);
    }
    $título = $lista->título;
}
?>

How do I generate an editing form loop for each specific XML data that is within the title and /title tags? (In the case of my loop-free input.php, I can only display the first list, as I can do to generate others other than it?

XML file: my_list.xml

<?xml version="1.0" encoding="UTF-8"?>
<banco-de-dados>
    <lista>
        <título>Título 1</título>
    </lista>
    <lista>
        <título>Título 2</título>
    </lista>
</banco-de-dados>

1 answer

0


Follows a Code that should work for what you want.

NOTE: Do not use special characters in code, whether PHP or XML. Special characters only for text to be read, otherwise avoid.

Change your XML to this one, where I removed the special character from the tag name.

<?xml version="1.0" encoding="UTF-8"?>
<banco-de-dados>
    <lista>
        <titulo>Título 1</titulo>
    </lista>
    <lista>
        <titulo>Título 2</titulo>
    </lista>
    <lista>
        <titulo>Título 3</titulo>
    </lista>
    <lista>
        <titulo>Título N</titulo>
    </lista>
</banco-de-dados>

For your list of titles, I don’t think you need a form for each item on the list (I would never use it like that). One is enough, but it will always change with the code displayed.

<form method="post">
<?php
    $carregador_xml = simplexml_load_file("minha_lista.xml");
    $i = 1;
    foreach ($carregador_xml->children() as $lista) { ?>        
    <input name="titulo[<?php echo $i++; ?>]" value="<?php echo $lista->titulo; ?>"> <br />    
<?php } ?>

    <input class="submitBtn" type="submit" name="submit" value="Salvar Registros!">
</form>

Already the code that writes back the changes can be this here

if (isset($_POST['titulo'])) {
    $titulos = (array) $_POST['titulo'];
    $carregador_xml = simplexml_load_file('minha_lista.xml');
    $funcao = fopen("minha_lista.xml", "wb");
    foreach ($titulos as $num => $titulo) {
        $i = 1;
        foreach ($carregador_xml->children() as $lista) {
            if ($i++ == $num ) {
                $lista->titulo = $titulo ; 
                break 1;
            }
        }       

    }
    fwrite($funcao, $carregador_xml->asXML());
    fclose($funcao);        

}

NOTE 2: If Titles will contain special characters in addition to accents, it may be necessary to use CDATA session, but simplexml does not support creating these sessions, requiring the use of DOM. I’ve used this solution here which I consider very elegant.

  • So I entered everything there, but when I put the value in the second input and click to send, it automatically sends the value of the second input to the first... has some method to make me generate a specific form for each tag list without the need to insert ids(nums) on each tag list? something like foreach or while to generate a loop of Formulars equal to the amount of list tags within xml.

  • I believe you are now as you wish. If you even decide to use a form for each, just put the form tag inside the loop, the code will still be valid.

  • great, it worked perfectly! I’m grateful for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.