I have a PHP script that allows me to modify the tags of an XML file according to an SQL query and to move this file to a folder once it has been processed.
I added a condition on an error, if the SQL query returns nothing I send an email to be notified. It works well but the file with the error is still moved to the folder. I would like this one to stay in the base folder and the script to continue on the other files.
I tried to change the order of execution of the script several times but without success… can you help me? Thanks
<?php
include "include/ODBCaccess.class.php";
$connect = odbc_connect("localhost","root","");
$dirname_source = "D:/xampp/htdocs/xml/";
$dirname_destination = "D:/xampp/htdocs/new_xml/";
$dir = opendir($dirname_source);
while($file = readdir($dir))
{
$source_file = $dirname_source.$file;
$destination_file = $dirname_destination."Order_".$file;
if(is_file($source_file))
{
// echo $source_file;echo "<br>";
// echo $destination_file;echo "<br>";
$xml =new DOMDocument("1.0","UTF-8");
$xml->load($source_file);
$xpath = new DOMXPath($xml);
foreach ($xpath->query("/Order/OrderLines") as $node)
{
$SKU_CODE = $node->getElementsByTagName("Code")->item(0)->nodeValue;
$query = "select GEAN from SKU where SKU='".$SKU_CODE."'";
// echo $query; echo "<br>";
$exec = odbc_exec($connect, $query);
$result = odbc_fetch_array($exec);
//ERROR
if ($result === false || $result['GEAN'] === null) {
// echo "GEAN not found for $SKU_CODE";
//email part
$to = "test@gmail.com.com";
$subject = "Error GEAN";
$message = "GEAN not found for $SKU_CODE in file $source_file";
$header = "From:noreply@gmail.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
}
// $barcode = (string) $result['GEAN'];
// echo $barcode; echo "<br>"; //9353970875729
$node->getElementsByTagName("SKU")->item(0)->nodeValue = "";
$node->getElementsByTagName("SKU")->item(0)->appendChild($xml->createTextNode($result['GEAN']));
}
$xml->formatOutput = true;
$xml->save($source_file);
rename($source_file,$destination_file);
}
}
closedir($dir);
?>
>Solution :
You can create a flag with default value as true, and in case of an error, set it to false, and only save the file if the flag is true, like shown below:
<?php
include "include/ODBCaccess.class.php";
$connect = odbc_connect("localhost", "root", "");
$dirname_source = "D:/xampp/htdocs/xml/";
$dirname_destination = "D:/xampp/htdocs/new_xml/";
$dir = opendir($dirname_source);
while ($file = readdir($dir)) {
$source_file = $dirname_source . $file;
$destination_file = $dirname_destination . "Order_" . $file;
if (is_file($source_file)) {
// echo $source_file;echo "<br>";
// echo $destination_file;echo "<br>";
$xml = new DOMDocument("1.0", "UTF-8");
$xml->load($source_file);
$xpath = new DOMXPath($xml);
$save = true; // default flag to allow saving
foreach ($xpath->query("/Order/OrderLines") as $node) {
$SKU_CODE = $node->getElementsByTagName("Code")->item(0)->nodeValue;
$query = "select GEAN from SKU where SKU='" . $SKU_CODE . "'";
// echo $query; echo "<br>";
$exec = odbc_exec($connect, $query);
$result = odbc_fetch_array($exec);
//ERROR
if ($result === false || $result['GEAN'] === null) {
$save = false; // set flag to false in case of error to avoid saving
// echo "GEAN not found for $SKU_CODE";
//email part
$to = "test@gmail.com.com";
$subject = "Error GEAN";
$message = "GEAN not found for $SKU_CODE in file $source_file";
$header = "From:noreply@gmail.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail($to, $subject, $message, $header);
if ($retval == true) {
echo "Message sent successfully...";
} else {
echo "Message could not be sent...";
}
}
// $barcode = (string) $result['GEAN'];
// echo $barcode; echo "<br>"; //9353970875729
$node->getElementsByTagName("SKU")->item(0)->nodeValue = "";
$node->getElementsByTagName("SKU")->item(0)->appendChild($xml->createTextNode($result['GEAN']));
}
if ($save) { // save if the flag is true
$xml->formatOutput = true;
$xml->save($source_file);
rename($source_file, $destination_file);
}
}
}
closedir($dir);
?>