0

我正在尝试构建一个 PHP 脚本,允许用户编辑谷歌地图的 XML 文件。我的脚本的灵感来自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");
    }
}
?>

XMLfile2.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 = 0; ...$i++; ,以便能够编辑每个单独的字段并提交更改,并且想知道如何处理它。也许那里有一个工作脚本,你可能知道我可以学习?帮助表示赞赏。谢谢

最接近的解决方案(浓缩):

<!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>
4

0 回答 0