Need to loop code on limit check

Codes here !

Moderators: macek, egami, gesf

Bluesplayer
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Mar 12, 2013 12:47 pm

Need to loop code on limit check

Postby Bluesplayer » Tue Mar 12, 2013 12:58 pm

Hi

I have a great many feeds to refresh. Sometimes these feeds will fail to connect properly and can fill my database up with nonsense. I have managed to lessen the damage caused by building in a limit check which works perfectly. What I would like to do is for the code to run another 2x at least when it hits the limit because in nearly all situations it will then connect to the feed correctly, whereas without a refresh (manual) the feed is deleted further on down the line. The code for refreshing the feeds is this:

Code: Select all

    // ------------------------------------------- refresh product feed
    } else if (isset($_GET['fd_refresh'])) {


    $fdrefresh = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'feedMemory".$_GET['fd_refresh']."'");
    $refresh = mysql_fetch_assoc($fdrefresh);

    $pieces = explode("-:-", $refresh['value']);
       
    $fdURLfeed = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'csvURL".$_GET['fd_refresh']."'");
    $URLfeed = mysql_fetch_assoc($fdURLfeed);
    $fdtypeFeed = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'csvType".$_GET['fd_refresh']."'");
    $typeFeed = mysql_fetch_assoc($fdtypeFeed);
    $fdcompressionFeed = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'compression".$_GET['fd_refresh']."'");
    $compressionFeed = mysql_fetch_assoc($fdcompressionFeed);

       if ($typeFeed['value'] == 'tab') {
       $typeFeed = "\t";
       } else {
       $typeFeed = $typeFeed['value'];
       }

    // set row variable and open file
    $row = intval($pieces[11]);
       if ($compressionFeed['value'] == 'gzip') {
       $handle = fopen('compress.zlib://'.$URLfeed['value'], "r");
       } else {
       $handle = fopen($URLfeed['value'], "r");
       }

    // empty the table
    $empty = "DELETE FROM affiliSt_products1 WHERE prodDB IN ('".$_GET['fd_refresh']."')";
    mysql_query($empty);

    // start auto inc from last ID in db
    $autoinc = mysql_query("SELECT prodID FROM affiliSt_products1 ORDER BY prodDB DESC LIMIT 1");
    $autoincnum = mysql_fetch_assoc($autoinc);
    $resetautoinc = mysql_query("ALTER TABLE affiliSt_products1 AUTO_INCREMENT = ".($autoincnum['prodID']+1)."");

    // while loop with fgetcsv sorts the csv into the data array
    while (($data = fgetcsv($handle, 3000, $typeFeed)) !== FALSE) {

    if ($row == 60000) {
        break 1;
    } 

       $resulta = $pieces[0];
       if ($data[intval($pieces[1])] == NULL) {
       $resultb = $row;
       } else {
       $resultb = $data[intval($pieces[1])];
       }
       $resultb = $data[intval($pieces[1])];
       $resultc = ucwords(strtolower($data[intval($pieces[2])]));
       $resultd = ucwords(strtolower($data[intval($pieces[3])]));
       $resulte = ucwords(strtolower($data[intval($pieces[4])]));
       $resultf = $data[intval($pieces[5])];
       $resultg = $data[intval($pieces[6])];
       $resulth = $data[intval($pieces[7])];
       $resulti = $data[intval($pieces[8])];
       $resultj = $data[intval($pieces[9])];
       $resultk = $pieces[10];
       $resultm = $data[intval($pieces[14])];
       $resultn = $data[intval($pieces[15])];
       $resulto = $data[intval($pieces[16])];
       $resultp = $data[intval($pieces[17])];
       $resultq = $data[intval($pieces[18])];
       $resultr = $data[intval($pieces[34])];
       $results = intval($row);
       
       if ($pieces[12] == 'before') {
       $resulth = $pieces[13].$resulth;
       } else if ($pieces[12] == 'after') {
       $resulth = $resulth.$pieces[13];
       }

    // remove characters that may interfear with navigation or display
       include("../includes/fixlist.inc.php");
       
       $resultc = str_replace($andAmps, " and ", $resultc);
       $resultd = str_replace($andAmps, " and ", $resultd);
       $resulte = str_replace($andAmps, " and ", $resulte);
       $resultc = str_replace($charquotes, "", $resultc);
       $resultd = str_replace($charquotes, "", $resultd);
       $resulte = str_replace($charquotes, "", $resulte);
       $resultb = str_replace($allCharacters, "-", $resultb);
       $resultc = str_replace($allCharacters, " ", $resultc);
       $resultd = str_replace($someCharacters, " ", $resultd);
       $resulte = str_replace($allCharacters, " ", $resulte);
       $resultj = str_replace($currencyCharacters, " ", $resultj);
       $resultl = $_GET['fd_refresh'];

    // miss out headers row
       if ($row != 0) {
       // insert all the data into the database table
          $sql = sprintf("INSERT INTO affiliSt_products1 (
        
                  merchant,   
                merchantProdID,    
                prodCategory,   
                prodName,    
                prodBrand,    
                prodDescription,    
                prodPromoText,    
                prodLink,    
                prodImageURL,    
                prodPrice,
                prodCurrency,
                prodDB,
                extraFieldA,
                extraFieldB,
                extraFieldC,
                extraFieldD,
                extraFieldE,    
                prodImageSmall,    
                dbProdID
                
                ) VALUES (
                
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s,
                %s
                )",
                quote_smart($resulta),
                quote_smart($resultb),
                quote_smart($resultc),
                quote_smart($resultd),
                quote_smart($resulte),
                quote_smart($resultf),
                quote_smart($resultg),
                quote_smart($resulth),
                quote_smart($resulti),
                quote_smart($resultj),
                quote_smart($resultk),
                quote_smart($resultl),
                quote_smart($resultm),
                quote_smart($resultn),
                quote_smart($resulto),
                quote_smart($resultp),
                quote_smart($resultq),
                quote_smart($resultr),
                quote_smart($results));
          mysql_query($sql, $databaseConnect) or die(mysql_error());
       }
       
       $row++;
    }


As you can see the above has a limit of 60000 entries and if that limit is reached it breaks out of the loop. How to make it repeat the full loop 2x say on hitting 60000 before it breaks out of the loop?

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Need to loop code on limit check

Postby johnj » Thu Mar 14, 2013 8:12 pm

I did not understand this fully but if you want the control to loop 2x times in hitting 6000,then please change the "if" condition accordingly.

Bluesplayer
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Mar 12, 2013 12:47 pm

Re: Need to loop code on limit check

Postby Bluesplayer » Fri Mar 15, 2013 1:11 am

I think I have this working now. I added a for loop at the beginning like this:

Code: Select all

// ------------------------------------------- refresh product feed
} else if (isset($_GET['fd_refresh'])) {

//  *** insert for loop here ***
for ($repeat = 0; $repeat < 3; $repeat++) {


Thanks


Return to “mySQL & php coding”

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests