Need some script help

Hey, I have this script on my site that used to work but recently stopped working and gives me 500 Internal Server Errors. I did some testing on the script and I can get it working if I stop the main loop half way in. I think I have too many images so it gets too big. The script looks at all the images and assigns them to products based on the image name and products model number. That way I can have multible images for products and its really easy to do. Can anyone look at my code and see how I could do one half stop, then do the second half?

case 'updateimages':
$files = array(); 
$handle = opendir( "../images/productimages" ); 

while($filename = readdir($handle)) 
{ 
    if ($filename == ".") {} 
    else if ($filename == "..") {} 
    else { 
  $files[] = $filename; 
    } 
} 
closedir($handle); 
sort( $files );
$result = count($files); 
$cnt = 0;
$search = tep_db_query("select products_id, products_model from " . TABLE_PRODUCTS . "");
while (($thissearch = tep_db_fetch_array($search)) && ($cnt < $result/2)) {
   $cnt++;
foreach( $files AS $name ) {
 if (($pos = strpos($name, ".")) !== false) {
      $filename = explode(".",$name);
  if (($pos2 = strpos($filename[0], "_")) !== false) {
   $pieces = explode("_",$filename[0]);
   if ($pieces[0] == $thissearch['products_model']) {
       if (($end = substr($pieces[1], -1)) == 'S') { 
    $location = 'productimages/';
    $location .= $name;
          $sql_data_array = array('products_id' => $thissearch['products_id'],
             'products_image' => $location);
            tep_db_perform(TABLE_PRODUCTS_IMAGES, $sql_data_array);
           } else {
    $location = 'productimages/';
    $location .= $name;
       $sql_data_array = array('products_id' => $thissearch['products_id'],
              'products_large_image' => $location);
            tep_db_perform(TABLE_PRODUCTS_LARGE_IMAGES, $sql_data_array);
    }
   }
       } else {
   $trim = rtrim($filename[0], "S");
   if ($trim == $thissearch['products_model']) {
       $thename = $filename[0];
    if ($thename[strlen($thename)-1] == 'S') {
    $location = 'productimages/';
    $location .= $name;
    $sql_data_array = array('products_id' => $thissearch['products_id'],
             'products_image' => $location);
            tep_db_perform(TABLE_PRODUCTS_IMAGES, $sql_data_array);
           } else {
    $location = 'productimages/';
    $location .= $name;
       $sql_data_array = array('products_id' => $thissearch['products_id'],
              'products_large_image' => $location);
            tep_db_perform(TABLE_PRODUCTS_LARGE_IMAGES, $sql_data_array);
    }
   }
  }
    }
}
      }
   $prosi = tep_db_query("select t1.products_id, t1.products_image from " . TABLE_PRODUCTS_IMAGES . " t1 inner join " . TABLE_PRODUCTS_IMAGES . " t2 on t1.products_id  = t2.products_id group by t1.products_id having t1.products_id = min(t2.products_id)"); 

while ($prods = tep_db_fetch_array($prosi)) {
  tep_db_query("update " . TABLE_PRODUCTS . " set products_image = '" . $prods['products_image'] . "' where products_id = '" . $prods["products_id"] . "'");
}

//update slave product pictures
$master_query = tep_db_query("select products_id, products_master from " . TABLE_PRODUCTS . " where products_master != 0");
while ($results = tep_db_fetch_array($master_query)) {
  $image_query2 = tep_db_query("select products_image from " . TABLE_PRODUCTS . " where products_id = '" . $results['products_master'] . "'");
  $image_query = tep_db_fetch_array($image_query2);
  tep_db_query("update " . TABLE_PRODUCTS . " set products_image = '" . $image_query['products_image'] . "' where products_id = '" . $results['products_id'] . "'");
} 
      break;

Thanks,
Ryan

nvm I figured it out.

Sponsor our Newsletter | Privacy Policy | Terms of Service