使用php从xml中删除整个元素

使用php从xml中删除整个元素,php,xml,Php,Xml,有没有一种简单的方法可以修改下面的php脚本,使其包含一个delete按钮?因此,对于循环中的每个项目,当前都有一个提交更改的按钮,我想添加一个按钮,该按钮可以简单地删除元素及其子元素 我是PHP新手,所以只要试着理解如何将它与XML结合使用,任何帮助都将不胜感激 这里是php <?php if(isset($_GET['e'])){ //If a date is submitted, edit it //var_dump($_GET);die; //Load the scores

有没有一种简单的方法可以修改下面的php脚本,使其包含一个delete按钮?因此,对于循环中的每个项目,当前都有一个提交更改的按钮,我想添加一个按钮,该按钮可以简单地删除元素及其子元素

我是PHP新手,所以只要试着理解如何将它与XML结合使用,任何帮助都将不胜感激

这里是php

<?php
if(isset($_GET['e'])){ //If a date is submitted, edit it
//var_dump($_GET);die;
    //Load the scores XML file
    $scores = new DOMDocument();
    $scores -> load('content.xml');

    //Get the <Games> tag
    $games = $scores -> getElementsByTagName('brand');

    //Loop through each found game
    foreach ($games as $game) {
        $child = $game->getElementsByTagName("id")->item(0);
        $oldTitle = $game->getElementsByTagName("title")->item(0);
        $oldScore = $game->getElementsByTagName("schedule_date")->item(0);
        $oldRow = $game->getElementsByTagName("image")->item(0);
        $id = $child->nodeValue;
        if($id==$_GET['e']){ //The date is the date asked for.
            $game -> replaceChild($scores -> createElement("title", $_GET['title']),$oldTitle);
            $game -> replaceChild($scores -> createElement("schedule_date", $_GET['schedule_date']),$oldScore);
            $game -> replaceChild($scores -> createElement("image", $_GET['image']),$oldRow);
        }
    }

    //Save again
    $scores -> save('content.xml');
}
?>
<hr>
<?php
//Load the scores XML file
$scores = new DOMDocument();
$scores -> load('content.xml');

//Get the <Games> tag
$games = $scores -> getElementsByTagName('brand');

//Loop through each game
foreach ($games as $game) {
    //Print each with an edit link.
    $id = $game->getElementsByTagName("id")->item(0)->nodeValue;
    $title = $game->getElementsByTagName("title")->item(0)->nodeValue;
    $score = $game->getElementsByTagName("schedule_date")->item(0)->nodeValue;
    $row = $game->getElementsByTagName("image")->item(0)->nodeValue;
    echo '<h3>'.$id . '</h3>
    <form method="get" action="">
        <input type="hidden" name="e" value="'.$id.'">
        Title: <input type="text" value="'.$title.'" name="title"><br>
        Score: <input type="text" value="'.$score.'" name="schedule_date"><br>
        Row: <input type="text" value="'.$row.'" name="image"><br>
        <input type="submit" value="edit">
    </form>
    <hr>';
}
?>


下面是一个xml示例

<brand>
<title></title>
<schedule_date></schedule_date>
<image></image>
</brand>

制作表单:

  <form method="get" action="">
    <input type="hidden" name="e" value="'.$id.'">
    Title: <input type="text" value="'.$title.'" name="title"><br>
    Score: <input type="text" value="'.$score.'" name="schedule_date"><br>
    Row: <input type="text" value="'.$row.'" name="image"><br>
    <input type="submit" value="edit">
  </form>
  <form method="post" action="">
     <input type="hidden" name="deleteid" value="'.$id.'">
     <input type="submit" name="action" value="delete">
  </form>
if(isset($_POST['action']) && $_POST['action']=='delete'){
   $scores = new DOMDocument();
   $scores -> load('content.xml');
   $xpath = new DOMXPath($scores);

   //you might want to clean up the $_POST['deleteid'], i don't know what's valid:
   $id = preg_replace('/[^a-z0-9]/i','',$_POST['deleteid']);

   //Searching nodes in DOM is best handled in xpath (unless getElementById() works)
   $search = $xpath->query('//id[.="'.$id.'"]/ancestor::game'); //use '/../'  if direct child

   if($search->length = 1){ //0 is not found, > 1 is multiple nodes!
       $deleteNode = $search->item(0);
       //this is the DOM way of deleting an item.
       $deleteNode->parentNode->removeChild($deleteNode);
   }
   $scores->save('content.xml');
}

呃,一个基本的
?不过我会给它自己的
POST
表单…嗨,Wrikken,谢谢你的评论-如果我理解正确,我知道如何包含html代码来创建按钮,我需要的帮助是附加到删除特定节点的代码