Click here for create dynamic XML using PHP part 1. Now this is part 2 in this tutorial i will show you how you can list and delete the xml data from your page. In the previous tutorial we have created the XML file with our form.
Listing the XML values to our page is easy and use very less code.
In the previous tutorial we have created a class – autoAssignerClass.php where we are writing our functions.Now we will add one more function that is listing the saved XML values in array.
public function listEntryFromXML($filename){ $xmlurl = $filename; if(file_exists($xmlurl)){ $xml = simplexml_load_file($xmlurl); $json = json_encode($xml); $array = json_decode($json,TRUE); if (count($array["Type"]) == count($array["Type"], COUNT_RECURSIVE)) { return $array; } else { return $array["Type"]; } } }
In the above code you can see we have used the simplexml to load the saved xml file. After getting the file we convert it to JSON so that it will be easy for us to use it in our PHP page. After the json encode we decoded the JSON and received the values in array. Finally after the checking our function will return the array.
For using this function we will include the class and create the object of the class to use its functions to our page where we want to show the list. You can see the previous tutorial where we have already created the object of the class. We call our function and get the list.
$listXML=$autoAssigner->listEntryFromXML($xmlFileName);
<table class="table table-bordered table-hover"> <thead> <tr> <th>Type</th><th>Message</th><th>Action</th> </tr> </thead> <tbody> <?php if(count((array)$listXML)){ foreach ($listXML as $value) { if($value["Name"]==""){}else{ echo '<tr><td>'.$value["Name"].'</td><td>'.$value["Message"].'</td><td><i class="fa fa-trash text-danger" onClick="deleteSms(\''.$value["Name"].'\')" data-toggle="tooltip" title="Delete SMS"></td></tr>'; }} } ?> </tbody> </table>
We use table to show the list which we get from XML.
After showing the list we need to delete the entry also. For this we have created the delete function.
public function deleteEntryFromXML($filename,$portal){ $xml = simplexml_load_file($filename); $toDelete = array(); foreach ($xml->Type as $item) { $price = $item->Name; if ($price == $portal ) { $toDelete[] = $item; } } foreach ($toDelete as $item) { $dom = dom_import_simplexml($item); $dom->parentNode->removeChild($dom); } $xml->asXML($filename); $message=new alertClass(); return $message->getAlert("Sms entry has been deleted", "success"); }
For triggering delete function we use javascript. To ask Yes and no
<script> function deleteSms(did){ $('#eid').val(did); var title=""; title="Are you sure you want to delete"; $('.modal-title').html(title); $('#mymodal').modal(); } </script> <div class="modal fade modal-default" id="mymodal"> <div class="modal-dialog modal-sm"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title">Modal Warning</h4> </div> <form action="" method="post"> <input hidden="" id="eid" name="eid" value=""> <input hidden="" id="statusid" name="statusid" value=""> <div class="modal-footer"> <button type="submit" id="deletesms" name="deletesms" class="btn btn-success">Yes</button> <button data-dismiss="modal" type="button" class="btn btn-danger">No</button> </div> </form> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div>
If user clicks on Yes it will call the delete method and delete the particular entry.
if(isset($_POST["deletesms"])){ $alert=$autoAssigner->deleteEntryFromXML("xmlManager.xml", $_POST["eid"]); }
autoAssignerClass.php
Full autoassignerclass.
<?php require_once( dirname( __FILE__ ) . '/../alert/alertClass.php' ); class autoAssignerClass { public function getXmlDetails($data,$filename){ if(file_exists($filename)){ $alert=$this->updateEntryToXML($data,$filename); } else { $alert=$this->createFirstTimeXML($data,$filename); } $message=new alertClass(); $msgExplode= explode(",", $alert); return $message->getAlert($msgExplode[0], $msgExplode[1]); } private function createFirstTimeXML($data,$filename){ $xmlDom=new DOMDocument(); $xmlDom->version="1.0"; $xmlDom->encoding='utf-8'; $xmlDom->formatOutput=true; $xmlFileName=$filename; $root=$xmlDom->createElement("AutoManager"); $xmNode=$xmlDom->createElement("Type"); $xmlDetailsNode=$xmlDom->createElement("Name",$data["Name"]); $xmNode->appendChild($xmlDetailsNode); $xmlDetailsNode=$xmlDom->createElement("Message",$data["Message"]); $xmNode->appendChild($xmlDetailsNode); $root->appendChild($xmNode); $xmlDom->appendChild($root); $xmlDom->save($xmlFileName); return "Sms has been added,success"; } private function updateEntryToXML($data,$filename){ $dom = new DOMDocument; $dom->load($filename); $dom->encoding='utf-8'; $dom->formatOutput=true; $xmlArr= $dom->getElementsByTagName('Name'); foreach ($xmlArr as $value) { $portalArr[]=$value->nodeValue; } if(in_array($data["Name"], $portalArr)){ return "Sms already exist,warning"; } else { $xmNode = $dom->createElement("Type", ""); $empName = $dom->createElement("Name", $data["Name"]); $xmNode->appendChild($empName); $empPortal = $dom->createElement("Message", $data["Message"]); $xmNode->appendChild($empPortal); $dom->getElementsByTagName("AutoManager")->item(0)->appendChild($xmNode); $dom->save($filename); return "Sms has been added,success"; } } public function deleteEntryFromXML($filename,$portal){ $xml = simplexml_load_file($filename); $toDelete = array(); foreach ($xml->Type as $item) { $price = $item->Name; if ($price == $portal ) { $toDelete[] = $item; } } foreach ($toDelete as $item) { $dom = dom_import_simplexml($item); $dom->parentNode->removeChild($dom); } $xml->asXML($filename); $message=new alertClass(); return $message->getAlert("Sms entry has been deleted", "success"); } public function listEntryFromXML($filename){ $xmlurl = $filename; if(file_exists($xmlurl)){ $xml = simplexml_load_file($xmlurl); $json = json_encode($xml); $array = json_decode($json,TRUE); if (count($array["Type"]) == count($array["Type"], COUNT_RECURSIVE)) { return $array; } else { return $array["Type"]; } } } }
Hope you like the tutorial – Create dynamic XML using php part -2 if you find any issue or having any advice then please comment below. Please share it with your friends.
No Comments