DotDragnet
February 08, 2012, 07:47:08 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Help decide how we fund DDN and where we take the community from here. Post in the thread all about it: http://www.dotdragnet.com/forum/index.php/topic,4368.0.html
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: More than 1 operation on if/else statement  (Read 729 times)
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« on: August 24, 2010, 08:46:44 AM »

I have this code at the bottom of a script which checks if the script is complete or not.

Code:
        //---------------------- WRITE THE FEED   
        fclose($handle);
        $feed_line_reload=$reload_line+100;
        echo "Processing from: ". $feed_line_reload . " to " . ($feed_line_reload + 99) ." product ids...";
        fwrite($handle_reload, $feed_line_reload);
        fclose($handle_reload);

    }
    catch(Exception $e){
        die($e->getMessage());
    }
    if (end($prodIds)<$reload_line){unlink('google_base_reload.txt');}
else {
    echo '<META http-equiv="refresh" content="1;URL=gbase.php">';
                   }

I have a mail function I would like to add in to that statement when the script is complete (on the 'if' part of the statement) but not quite sure how you'd add in a second function to a part of an if/else statement. Would this work?

Code:
    if (end($prodIds)<$reload_line){unlink('google_base_reload.txt');
       mail('paul@blah.co.uk', 'Google Base Complete', 'The Google Base export feed is now complete.');}
             else {
       echo '<META http-equiv="refresh" content="1;URL=gbase.php">';
                   }
Logged

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



View Profile WWW Awards
« Reply #1 on: August 24, 2010, 10:35:27 AM »

Ok, so tried it and it works.

My next thought was to automate the whole process via cron but for some reason the script doesn't like running that way, only via a browser.

Can anyone point me towards where I should be looking in the code to see why it doesn't like being run via cron so I can investigate further?

Code:
<?php
    define
('SAVE_FEED_LOCATION','google_base_feed.txt');//you can set a new folder and file if you want, don't forget to chmod the folder to 777
    
define('SAVE_FEED_RELOAD','google_base_reload.txt');//you can set a new folder and file if you want, don't forget to chmod the folder to 777

    // Set the VAT rate as Google Base requires prices including VAT and we show them on the site excluding VAT. Rate set as a decimal not percentage
    
$vat=1.175;

    
// make sure we don't time out
    
set_time_limit(0);    

    require_once 
'/home/myoffice/public_html/app/Mage.php';
        
Mage::app('default');
        
    try{
        
        if (
file_exists(SAVE_FEED_RELOAD)) {$reload_line=file_get_contents(SAVE_FEED_RELOAD);} else {$reload_line=0;}
        if (
$reload_line==0){$handle fopen(SAVE_FEED_LOCATION'w');} else {$handle fopen(SAVE_FEED_LOCATION'a+');}
        
$handle_reload fopen(SAVE_FEED_RELOAD'w');
        
        
        if (
$reload_line==0){
            
$heading = array('id','title','description','link','image_link','price','product_type','condition''c:product_code');
            
$feed_line=implode("\t"$heading)."\r\n";
            
fwrite($handle$feed_line);
        }
        
//---------------------- GET THE PRODUCTS    
        
$products Mage::getModel('catalog/product')->getCollection();
        
$products->addAttributeToFilter('status'1);//enabled
        
$products->addAttributeToFilter('visibility'4);//catalog, search
        
$products->addAttributeToSelect('*');
        
$prodIds=$products->getAllIds();
        
        
//echo 'Product filter: '.memory_get_usage(false).'<br>';
        //flush();
        
        //$i=0;
        
foreach($prodIds as $productId) {
            
            if ((
$productId>=$reload_line) & ($productId<$reload_line+100)){
            
$product Mage::getModel('catalog/product');
            
            
//echo '. ';
            //flush();
            //echo 'Loop start: '.memory_get_usage(false).'<br>';
            //flush();
    
            //$product = Mage::getModel('catalog/product');
            
$product->load($productId);
            
            
$product_data = array();    
            
$product_data['sku']=$product->getSku();    
            
$product_data['title']=$product->getName();
            
$product_data['description']=$product->getShortDescription();
            
$product_data['link']=$product->getProductUrl(). '?source=googleps';
            
$product_data['image_link']=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage();
            

// Get price of item
if($product->getSpecialPrice())
                
$product_data['price']=$product->getSpecialPrice();
            else
   $product_data['price']=$product->getPrice();

// Check if item subject to VAT and add on for Google requirements
            
$tax_class=$product->getTaxClassId();
/* Check tax code and add vat if necessary */
if ($tax_class=='2') {
$product_data['price'] = str_replace('"'''$product_data['price']) * $vat;
}
unset($tax_class);

// Round price to 2 decimal places
$product_data['price']=round($product_data['price'], 2);


            
//$product_data['brand']=$product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($product);    
            
$product_data['product_type']='';
            
$product_data['condition']='new';
            
$product_data['c:product_code']=$product->getSku();
                
          
            
//echo 'Product load: '.memory_get_usage(false).'<br>';
            //flush();        
          
            //get the product categories                    
                    
foreach($product->getCategoryIds() as $_categoryId){
            
$category Mage::getModel('catalog/category')->load($_categoryId);
            
$product_data['product_type'].=$category->getName().', ';
            }
            
$product_data['product_type']=rtrim($product_data['product_type'],', ');        

            
//echo 'Category load: '.(memory_get_usage(false)).'<br>';            
            
            //sanitize data    
            
foreach($product_data as $k=>$val){
            
$bad=array('"',"\r\n","\n","\r","\t");
            
$good=array(""," "," "," ","");
            
$product_data[$k] = '"'.str_replace($bad,$good,$val).'"';
            }
            

            
$feed_line implode("\t"$product_data)."\r\n";
            
fwrite($handle$feed_line);
            
fflush($handle);
            
            
//echo 'Loop end: '.memory_get_usage(false).'<br>';
            //flush();
            
            
}
        }

        
//---------------------- WRITE THE FEED    
        
fclose($handle);
        
$feed_line_reload=$reload_line+100;
        echo 
"Processing from: "$feed_line_reload " to " . ($feed_line_reload 99) ." product ids...";
        
fwrite($handle_reload$feed_line_reload);
        
fclose($handle_reload);

    }
    catch(
Exception $e){
        die(
$e->getMessage());
    }
    if (
end($prodIds)<$reload_line){unlink('google_base_reload.txt');
        
mail('paul@blah.co.uk''Google Base Complete''The Google Base export feed is now complete.');}
else {
    echo 
'<META http-equiv="refresh" content="1;URL=gbase.php">';
                   }
?>
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 #2 on: August 24, 2010, 12:23:52 PM »

Didn't you ask this before?

chdir(dirname(__FILE__));

Near the top, or set your SAVE_FEED_* constants with the complete path.
Logged
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #3 on: August 24, 2010, 01:25:10 PM »

Thanks Jason.

Before I was creating a file to check if another file existed, so it was slightly different. I'm not the best with php as you've guessed but getting the hang of some bits slowly.

Thanks for your help.
Logged

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



View Profile WWW Awards
« Reply #4 on: August 24, 2010, 04:53:04 PM »

The issue with this file is when you run it via cron, it doesn't run properly so I wondered if there should be much that has to be changed for a file to run via cron. I believed that if it ran via browser call then it should pretty much be able to run via cron but doesn't seem that way.

I think it's because the script refreshes the browser in the if/else statement and this wouldn't happen when a cron runs the script, so what I would have to do is amend the script to go back to the start programatically instead.

EDIT: I'm looking at using the header function to redirect back to the top now, hopefully that will solve the issue.
« Last Edit: August 24, 2010, 05:27:02 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 #5 on: August 24, 2010, 09:41:35 PM »

You could possibly replace the refresh with exec() (check manual page and comments) and change nothing else.

header won't solve a thing.

If the only reason for running it a piece at a time is that it takes a while and the browser will time out, then that shouldn't apply to being run by cron.

If it is hitting max_execution_time you need to add set_time_limit(0);.

If it's running out of memory you need to increase memory_limit (I'm not sure if you can do that at runtime or if it has to be in php.ini), or you need some more efficient code.
Logged
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #6 on: August 25, 2010, 07:28:15 AM »

Thanks Jason,

Gives me something to look at.
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!