Hi all
I developed and run a PHP application for my company which displays a front end to a MySQL database so suppliers can upload their stock lists to my FTP server and it is displayed on the front end if a salesman searches for something.
I am in the process or rewriting it from the ground up as over the years where bits have been added and deleted, it is 2000 lines of hell!
So, to tidy it up I am trying to recode using arrays, as a lot of processes are just repeated multiple times for different suppliers - of which there are about 30. So rather than writing the same process over and over for each suppliers file, an array seemed the best way.
One process I have is to check if the datafile uploaded from the supplier is newer than the data in the application and if it is, set a variable to 1 so that suppliers data can be uploaded. I have created an array which contains SQL scripts to obtain the timestamp from the database, variables to hold the timestamp of the latest file provided by the supplier and a flag variable to show 0 for no update required, or 1 for update needed.
When the script runs… it works perfectly in as much as it correctly compares the database time stamp to the FTP file timestamp and reports whether an update is needed, or not. The flag variable in application is $import_supplier1, $import_supplier2, etc. In the array this variable is called $import_supplier and in the foreach loop correctly shows 1 if an update is needed and 0 if not needed.
But I am struggling to work out how to store the result obtained in the loop back to $import_supplier1, $import_supplier2, etc so that further on in the script I can perform the update if needed.
The only way I can see to do this is to incorporate the update process in the loop as well… I have looked at the PHP Help, but havent worked it out. Can anyone offer any simple advice? I am not a proficient coder, so simple as possible would be good
Below is an excerpt of the script I am working on…
<?PHP
// Define variable for file timestamp
$file_timestamp_supplier1 = filemtime ("/var/www/clients/client1/web1/web/distcheck/datafiles/FTP/supplier1/datafile.csv");
$file_timestamp_supplier2 = filemtime ("/var/www/clients/client1/web1/web/distcheck/datafiles/FTP/supplier2/datafile.csv");
// Define flag variable to be 0 for no update required
$import_supplier1 = 0;
$import_supplier2 = 0;
// Connect to SQL database
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'database';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if($mysqli->connect_errno) {
printf("Connect failed: %s<br />", $mysqli->connect_error);
exit();
}
printf('Connected successfully to database.</br></br>');
// Array to compare timestamp of existing file from SQL Database and timestamp of supplier file on FTP server to determine whether update is neccessary - set $import_supplier1 / $import_supplier2 to 1 if needed
$array = [
["SELECT `timestamp` FROM `timestamp` WHERE `supplier` = 'Supplier 1'", &$db_timestamp_supplier1, &$file_timestamp_supplier1, &$import_supplier1, "Supplier 1"], //Supplier 1
["SELECT `timestamp` FROM `timestamp` WHERE `supplier` = 'Supplier 2'", &$db_timestamp_supplier2, &$file_timestamp_supplier2, &$import_supplier2, "Supplier 2"], //Supplier 2
];
foreach ($array as list($sql_code, $db_timestamp_supplier, $file_timestamp_supplier, $import_supplier, $supplier)) {
$result=mysqli_query($mysqli,$sql_code);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
$db_timestamp_supplier = $row["timestamp"];
if ($db_timestamp_supplier != $file_timestamp_supplier) {
$import_supplier = 1 ;
echo "<b><font color= 'red' face='Arial'></br>".$supplier." needs updating</b> (file timestamp = ".$file_timestamp_supplier." / database timestamp = ".$db_timestamp_supplier." / update flag = ".$import_supplier.")</face></br></br>";
} else {
$import_supplier = 0;
echo "<font color= 'green' face='Arial'>".$supplier." is up to date (file timestamp = ".$file_timestamp_supplier." / database timestamp = ".$db_timestamp_supplier." / update flag = ".$import_supplier.")</face></br>";
}
}
?>