title: PHP break up for each loop in Magento Unicenta product update script
body: <p>I have a ForEach loop in an extension for Magento that is hitting a hard-wall with my shared host MySQL server. The script is taking too long to execute and the MySQL server shuts down the connection. Obviously I cannot just have the hosting platform increase the time-out, because as more products are added, that time-out would need to be increased. The only viable option I can see is to split up the foreach loop.</p>
  
  <p>Here is the function:</p>
  
  <pre><code>$ps=Mage::getModel('unicentaopos/productstock');
  $ps-&gt;loadProductHash();//load a hash of history
  $ps-&gt;noImageProducts();//load products without images so that if change is detected we load them.
  
  if (is_a($mode,'Mage_Cron_Model_Schedule')){
      $ps-&gt;configSim();
  }else{
      $ps-&gt;configSim($mode);
  }
  $db=Mage::Helper('unicentaopos')-&gt;getUnicentaOposConnection();
  $productsTable=Mage::Helper('unicentaopos')-&gt;getTableName(self::TABLE_PRODUCTS);
  if (is_null($db))return false;
  $sql="select * from `$productsTable`";
  $rows=$db-&gt;query($sql);
  if (is_null($rows)) return false;
  
  foreach  ($rows as $row){
      $ps-&gt;getUnicentaProducts($row);
      $ps-&gt;doImage($row);
  }
  $ps-&gt;updateMagentoProducts();
  return true;
  </code></pre>
  
  <p>Now the function currently has a lot of logging enabled, and from what I can tell in the log it gets to item number 1017 before it errors out each time. There are currently 1088 products/items but that number will grow. I was thinking to split it up so that it processes no more than say 500 per round into the database, then picks up where it left off and does another 500, but when it reaches the true end it stops. Can anyone help me figure this out, I know programming in a few languages but this is completely foreign to me.</p>