orionvictor Posted January 23, 2013 Share Posted January 23, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/273536-php-simple-xml-query-writing-accessing-date/ Share on other sites More sharing options...
orionvictor Posted January 27, 2013 Author Share Posted January 27, 2013 any help is appreciated Quote Link to comment https://forums.phpfreaks.com/topic/273536-php-simple-xml-query-writing-accessing-date/#findComment-1408557 Share on other sites More sharing options...
salathe Posted January 27, 2013 Share Posted January 27, 2013 There is an error on this line... if ($item->id = $id) Can you spot it? Quote Link to comment https://forums.phpfreaks.com/topic/273536-php-simple-xml-query-writing-accessing-date/#findComment-1408610 Share on other sites More sharing options...
orionvictor Posted January 29, 2013 Author Share Posted January 29, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/273536-php-simple-xml-query-writing-accessing-date/#findComment-1408923 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.