0

I'm trying to build a PHP script that allows the user to edit an XML file for Google Maps. My script is inspired by How to modify xml file using PHP

<?php
$xml = new DOMDocument('1.0','UTF-8');
$xml->preserveWhiteSpace = FALSE;
            
$xml->load("file2.xml");
        
$mar = $xml->getElementsByTagName("marker");
foreach($mar as $row){
    
    $name = $row->getElementsByTagName('name')->item(0);
    $lat = $row->getElementsByTagName('lat')->item(0);
    $lng = $row->getElementsByTagName('lng')->item(0);
    $category = $row->getElementsByTagName('category')->item(0);
    $type = $row->getElementsByTagName('type')->item(0);
    $id = $row->getElementsByTagName('id')->item(0);
    $desc = $row->getElementsByTagName('desc')->item(0);
    $img = $row->getElementsByTagName('img')->item(0);

    $row->replaceChild($name, $name);
    $row->replaceChild($lat, $lat);
    $row->replaceChild($lng, $lng);
    $row->replaceChild($category, $category);
    $row->replaceChild($type, $type);
    $row->replaceChild($id, $id);
    $row->replaceChild($desc, $desc);
    $row->replaceChild($img, $img);
?>
            
<form id="q_form" method="post" action="">
    <div class="form-group form-group-lg">
        <div class="table-responsive">
            <table id="choices_table"  class="table table-bordered"  >
                <tr>
                   
                    <td>
                    <div class="form-group">
                        <input type="text"
                                name ="name" 
                                value="<?php echo $name->nodeValue ?>"
                                />
                     </div>
                    </td>
                    <td>
                    <div class="form-group">
                        <input type="lat"
                                name ="lat" 
                                value="<?php echo $lat->nodeValue ?>"
                                />
                     </div>
                    </td>
                    <td>
                    <div class="form-group">
                        <input type="lat"
                                name ="lng" 
                                value="<?php echo $lng->nodeValue ?>"
                                />
                     </div>
                    </td>
                    <td>
                    <div class="form-group">
                        <input type="lat"
                                name ="category" 
                                value="<?php echo $category->nodeValue ?>"
                                />
                     </div>
                    </td>
                    <td>
                    <div class="form-group">
                        <input type="text"
                                name ="type" 
                                value="<?php echo $type->nodeValue ?>"
                                />
                        </div>
                    </td>
                    <td>
                    <div class="form-group">
                        <input type="text"
                                name ="id" 
                                value="<?php echo $id->nodeValue ?>"
                                />
                     </div>
                    </td>
                    <td>
                    <div class="form-group">
                        <input type="text"
                                name ="desc" 
                                value="<?php echo $desc->nodeValue ?>"
                                />
                     </div>
                    </td>
                    <td>
                    <div class="form-group">
                        <input type="text"
                                name ="img" 
                                value="<?php echo $img->nodeValue ?>"
                                />
                     </div>
                    </td>
                  
                   
        </table>
        </div>  
    
</div>
    <input type="submit"  name="submit" value=" EDIT "  class="btn btn-primary" />
</form>  
<?php
    if (isset($_POST['submit'])) {
        $name->nodeValue = $_POST['name'];
        $lat->nodeValue = $_POST['lat'];
        $lng->nodeValue = $_POST['lng'];
        $category->nodeValue = $_POST['category'];
        $type->nodeValue = $_POST['type'];  
        $id->nodeValue = $_POST['id']; 
        $desc->nodeValue = $_POST['desc'];   
        $img->nodeValue = $_POST['img'];
    
        $xml->save("file2.xml");
    }
}
?>

XML file2.xml

<markers>
   <marker>
     <name>name 12</name>
     <lat>lat 1</lat>
     <lng>lng 1</lng>
     <category>cat 1</category>
     <type>type 1</type>
     <id>id 1</id>
     <desc>desc 1</desc>
     <img>/path/1.png</img>
  </marker>
  <marker>
    <name>name 12</name>
    <lat>lat 1</lat>
    <lng>lng 1</lng>
    <category>cat 1</category>
    <type>type 1</type>
    <id>id 1</id>
    <desc>desc 1</desc>
    <img>/path/1.png</img>
   </marker>
</markers>

I need to introduce an array or a count e.g. $i = 0; ...$i++; so as to be able to edit each individual field and submit the change and was wondering how to approach it. Perhaps there's a working script out there you may know of that I could study? Help appreciated. Thanks

Closest solution (condensed):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>XML Editor</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
    <?php
$xml = new DOMDocument('1.0','UTF-8');
$xml->preserveWhiteSpace = FALSE;
            
$xml->load("file2.xml");
        
$mar = $xml->getElementsByTagName("marker");
$count = 0;
foreach($mar as $i => $row){

            $lat = $row->getElementsByTagName('lat')->item(0);
            $row->replaceChild($lat, $lat);
            $count++;
            ?>
            
<form id="q_form" method="post" action="">
    <div class="form-group form-group-lg">
        <div class="table-responsive">
            <table id="choices_table"  class="table table-bordered"  >
                <tr>
                    <td>
                    <div class="form-group">
                        <input type="text"
                                name ="<?php echo 'lat['.$i.']'; ?>" 
                                value="<?php echo $lat->nodeValue ?>"
                                />
                     </div>
                    </td>
                </tr> 
        </table>
        </div>  
    
</div>
    <input type="submit"  name="submit<?php echo $count; ?>" value=" EDIT <?php echo $count; ?>"  class="btn btn-primary" />
</form>  


    <?php
    if (isset($_POST['submit' . $count]))
    echo "count: " . $count;
    {
        @$lat->nodeValue = $_POST['lat'][$count];
        print_r($_POST);
        // var_dump($lat);
        $xml->save("file2.xml");
        }
    
}
?>
</body>
</html>
User301276
  • 55
  • 1
  • 8
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Oct 28 '21 at 12:10
  • 1
    `foreach($mar as $i => $row){` then `name =""` – RiggsFolly Oct 28 '21 at 12:17
  • What have you tried so far? Anything not working when you use a common `for` loop fo rthis? – Nico Haase Oct 28 '21 at 12:27
  • That's great thanks. What would I change `$lat->nodeValue = $_POST['lat'];` this to? Very close to working as intended – User301276 Oct 28 '21 at 12:28
  • Just need to resolve a `Notice: Array to string conversion in` issue with ` $lat->nodeValue = $_POST['lat'];` – User301276 Oct 28 '21 at 12:49
  • Well you now have an array of `$_POST['lat']` So you have `$_POST['lat'][0]` and `$_POST['lat'][1]` – RiggsFolly Oct 28 '21 at 12:52
  • Ok thanks. Testing with `$lat->nodeValue = $_POST['lat'][0];` presents me with the `Notice: Undefined offset: 0` error. – User301276 Oct 28 '21 at 12:56
  • Do a `print_r($_POST)` and show that – RiggsFolly Oct 28 '21 at 12:59
  • `Notice: Undefined offset: 0 ...` and `print_r($_POST)` output is: `Array ( [lat] => Array ( [1] => ) [submit] => EDIT ) ` . I also appended the condensed solution so far to the original question. I possibly need a foreach loop for the output? – User301276 Oct 28 '21 at 13:15
  • adding `@` helps to fix error e.g. `@$lat->nodeValue = $_POST['lat'][0];` inspired by https://stackoverflow.com/questions/7688814/how-to-avoid-php-notice-undefined-offset-0-without-checking-each-field-of-arr . I believe I may need to add a loop now to get the right output – User301276 Oct 28 '21 at 13:35
  • Where I gave you `name =""` It was an example of what you had to do to ALL the fields on that form apart from the submit bitton – RiggsFolly Oct 28 '21 at 13:53
  • Yes. I just want to get it working first for 1 field `lat` and then I'll apply to other fields. – User301276 Oct 28 '21 at 14:02
  • how do I save an array value to file.xml. `$xml->save("file2.xml");` doesn't work – User301276 Oct 28 '21 at 15:20

0 Answers0