I wanna change a line in a XML file that contains a code obtained via POST.
I am searching for a way to find the file on XML files' directory that contains the code, for then change the line.
Can someone help me?
My PHP code:
$files = scandir('xml/.'); // List files
foreach($files as $file) {
if($file == '.' || $file == '..') continue;
$xml = simplexml_load_file("xml/".$file);
$result = $xml->xpath("pedido");
if($result[3]->numpedido == $dilma) {
echo "It exists!";
}
}
/* My XML Looks like this
<pedido>
<cliente>João</cliente>
<mesa>34</mesa>
<hora>13:01:10</hora>
<numpedido>6780110</numpedido>
<lista>Baiao, Frango, Porco, e Macaxeira</lista>
<status>Aberto</status>
</pedido>
*/
Edit: The error that I am getting is:
Notice: Undefined offset: 3 in /var/www/html/restaurante/cmae/rita.php on line 43
Notice: Trying to get property of non-object in /var/www/html/restaurante/cmae/rita.php on line 43
$result = $xml->xpath("pedido");
will not return any result. You need
to change your xpath to $result = $xml->xpath("/pedido");
. That way you will have an array with simple XML elements (the handling of result needs to be different).
if (isset($result[0]) && isset($result[0]->numpedido) && $result[0]->numpedido == $dilma) {
echo 'exists';
}