Jump to content

orionvictor

New Members
  • Posts

    5
  • Joined

  • Last visited

orionvictor's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. The code works as a champ! Easy solution with parentNode->removeChild functionality. Hope it might help someone else too.
  2. XML file: <?xml version="1.0" ... > <database xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <record> <id>1</id> <name></name> </record> <record> ... </record> </database> I want to delete the record where <id> equals $_GET['id']. I tried many suggested implementations based on xml or simplexml, without result. Came across a very simple workaround (it should omit the record where id=$id), but can someone help me to taylor it to my xml scheme? <?php $file = "database.xml"; $id = $_GET['id']; echo "DELETING: ".$id; $xml = simplexml_load_file($file); $str = "<?xml version=\"1.0\"?> <database>"; foreach($xml->children() as $child){ if($child->getName() == "id") { if($id == $child['name']) { continue; } else { $str = $str.$child->asXML(); } } } $str = $str." </database>"; echo $str; $xml->asXML("database_backup.xml"); $fh = fopen($file, 'w') or die("can't open file"); fwrite($fh, $str); fclose($fh); ?>
  3. This is what I mean with community help. Also, more sleep would do well. A double equal sign should be used instead of a single one: if ($item->id == $id) The code works well now. Thanks
  4. Hello, I try to create a database, where you can retrieve one-time passwords, and displays if a password has been used (queried) before by adding a retrieval date. (Later I would like to delete the record after it has been accessed for a second time). I created otp.xml (database): <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <otp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <record> <id>DIGITATE</id> <code>1BBEDC977DE14DFE866BDAC20C521B69</code> </record> <record> <id>MADHOUSE</id> <code>5ADA911C909F5752CE9C9FE708370E91</code> </record> <record> <id>METE</id> <code>736047641CB11B6DFB49F4776CAB59B4</code> </record> ... Now I would like to retrieve the record information based on a query of the 'id'. My otp.php: <form action="otp.php" method="post"> Id: <input name="id" type="text" /> <input type="submit" /> </form> <?php $id = $_POST['id']; $file = "otp.xml"; $found = false; $xml = simplexml_load_file($file); //get data from XML tags and store in array foreach($xml->record as $item) { if ($item->id = $id) { $found = true; $info = array( "id" => (string)$item->id, "code" => (string)$item->code, "dtg" => (string)$item->dtg ); if ($item->dtg == "") { $item->dtg = date('d/m/Y H:i:s e (D)'); $xml->asXML($file); } else { break; } break; } } if($found){ echo "<pre>"; print_r($info); echo "</pre>"; echo "<br />"; print $info["id"]; echo "<br />"; print $info["code"]; echo "<br />"; print $info["dtg"]; echo "<br />"; }else{ // Item not found echo "<p>Not found in database.</p>"; } ?> So far so good, it retrieves data. Problem 1: It only retrieves the ID, and displays the code and dtg (date/time group) of the first xml record. Problem 2: It only writes the dtg to the first record. I want to retrieve directly from the id, not from <record id="DIGITATE">, as I want to create my xml file from excel. Other solutions don't work: foreach($xml->record as $item) { if ($item->id = $id) { $found = true; $info = array( "id" => (string)$item->id, "code" => (string)$item->code, "dtg" => (string)$item->dtg ); if ($info["dtg"] == "") { $otp = new SimpleXMLElement('otp.xml',null,true); $record = $otp->xpath('/otp/record[id=.$id]'); $record[0]->dtg .= date('d/m/Y H:i:s e (D)'); header("Content-type: text/xml"); echo $otp->asXML(); } else { break; } break; } } foreach($xml->record as $item) { if ($item->id = $id) { $found = true; $info = array( "id" => (string)$item->id, "code" => (string)$item->code, "dtg" => (string)$item->dtg ); if ($item->dtg == "") { $xml->record->dtg = date('d/m/Y H:i:s e (D)'); $xml->asXML('otp.xml'); } else { break; } break; } } foreach($xml->record as $item) { if ($item->id = $id) { $found = true; $info = array( "id" => (string)$item->id, "code" => (string)$item->code, "dtg" => (string)$item->dtg ); if ($item->dtg == "") { $item->addChild('dtg', date('d/m/Y H:i:s e (D)')); $xml->asXML('otp.xml'); } else { break; } break; } } Where is my logical mistake?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.