Delete whole element from xml with php -
is there simple way amend following php script include delete button - each item in loop there button submit changes, add button removes element , children.
i new php trying understand how use in combination xml - appreciated.
here php
<?php if(isset($_get['e'])){ //if date submitted, edit //var_dump($_get);die; //load scores xml file $scores = new domdocument(); $scores -> load('content.xml'); //get <games> tag $games = $scores -> getelementsbytagname('brand'); //loop through each found game foreach ($games $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 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 scores xml file $scores = new domdocument(); $scores -> load('content.xml'); //get <games> tag $games = $scores -> getelementsbytagname('brand'); //loop through each game foreach ($games $game) { //print each 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>'; } ?>
an here example xml
<brand> <title></title> <schedule_date></schedule_date> <image></image> </brand>
making form:
<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>
using form:
if(isset($_post['action']) && $_post['action']=='delete'){ $scores = new domdocument(); $scores -> load('content.xml'); $xpath = new domxpath($scores); //you might want clean $_post['deleteid'], don't know what's valid: $id = preg_replace('/[^a-z0-9]/i','',$_post['deleteid']); //searching nodes in dom best handled in xpath (unless getelementbyid() works) $search = $xpath->query('//id[.="'.$id.'"]/ancestor::game'); //use '/../' if direct child if($search->length = 1){ //0 not found, > 1 multiple nodes! $deletenode = $search->item(0); //this dom way of deleting item. $deletenode->parentnode->removechild($deletenode); } $scores->save('content.xml'); }
Comments
Post a Comment