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>