DotDragnet
February 08, 2012, 08:05:12 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Mobile users - Our forum is Tapatalk enabled. http://www.tapatalk.com/
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: PHP Script. Count and email updated rows  (Read 501 times)
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« on: May 21, 2010, 07:34:19 PM »

The following code updates products in a db by opening a csv file and updating a product already in the db using the newly update prices in the csv file.

Now we've been running the file for some time manually I wish to make it automated via cron so have added a line at the end to mail me when the script is complete. How would I look to add a count to this script so that the email message can tell me how many products were updated and maybe even email me the log that is created as that tells me which products weren't updated?

EDIT: Think the answer is already in the script near the bottom but I cannot work out how to echo that figure on my email...

To add as well, it is currently missing the first row of the file. How can I change this as there is no header rom in my data?
 
Code:
<?php
/* Markup percentage */
$percent 0.xx;

if(
is_file('./log/eos_update.log'))
{
        
rename('./log/eos_update.log''./log/eos_update-'.time().'.log');
}


$proxy = new SoapClient('http://myofficestationery.com/api/soap/?wsdl=1');
$sessionId $proxy->login('import''import');

$handle fopen('eos.csv'"r");
$i=0;
echo 
"Started\n";
$failed_stack = array();
while(
$data fgetcsv($handle1000",","\""))
{
if($i!=0) {
$product_code trim($data[0]); // not necessary but it cast $data[0] to string (just to ensure we're updating by SKU and not by ID)
$price $data[4];
$price $price $percent;
        $price number_format($price2'.''');
echo $product_code.' - '.$price;
try {
$proxy->call($sessionId'product.update', array($product_code, array('price'=>$price)));
} catch (SoapFault $e) {
array_push($failed_stack$product_code);
echo 'Unable to find: '.$product_code.' - skipped and not updated';
}
echo "\n";
}
++$i;

}

if(
count($failed_stack) > 0) {
$output "Failed: ".count($failed_stack)."\n";
foreach($failed_stack as $key => $value) {
$output .= "   +----- ".$value."\n"
}
file_put_contents('./log/eos_update.log',$output); 
}

rename('eos.csv''./log/eos.csv-'.time());


echo 
"Done\n";

/* Send email to admin when script complete */

mail('paul@email.co.uk''Weekly EOS Update''The weekly EOS update is complete.');

fclose($handle);
?>
« Last Edit: May 21, 2010, 08:48:06 PM by Whatever » Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
JasonD
Global Moderator
Hero Member
*****
Posts: 529



View Profile Awards
« Reply #1 on: May 21, 2010, 09:55:13 PM »

Quote
How can I change this as there is no header rom in my data?

Remove the if($i!=0) {, and the corresponding } above ++$i.

Quote
How would I look to add a count to this script so that the email message can tell me how many products were updated and maybe even email me the log that is created as that tells me which products weren't updated?

$updated = $i - count($failed_stack);
mail('paul@email.co.uk', 'Weekly EOS Update', 'The weekly EOS update is complete.'
  . "\nUpdated: $updated\n" . $output);

Logged
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #2 on: May 22, 2010, 05:40:05 AM »

Thanks Jason. Will give those a whirl when back from site later today.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!