Jump to content

PHP Simple XML query + writing accessing date


orionvictor

Recommended Posts

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.