Jump to content

[SOLVED] function stuck in loop


Schlo_50

Recommended Posts

Hey guys,

 

I have a block of code that needs to be executed once if a certain value exists.

 

The problem is that the code to be executed is in a foreach loop and uses a function, so the function runs more than once giving me the error: Fatal error: Cannot redeclare find_by_colum() ..

 

Has anybody got any ideas for this? Code is below (condensed):

 

<?php

$bfile = file("admin/data/threads.DAT");
  foreach($bfile as $bKey => $Val){
   $Data[$bKey] = explode("|", $Val);
   if(trim($Data[$bKey][2]) != trim($_GET['cid']) && trim($Data[$bKey][14]) != trim($_GET['cid']) && trim($Data[$bKey][15]) != trim($_GET['cid']) && trim($Data[$bKey][16]) != trim($_GET['cid']) && trim($Data[$bKey][17]) != trim($_GET['cid'])){
   print "";
   }
   else { 
   
  function find_by_colum($array, $column, $query) {
  $matches = array();
   foreach ($array as $value) {
    $parts = explode('|', $value);
 $cid = trim($_GET['cid']);
     if ($parts[$column-1] == $query || $parts[14] == $cid || trim($parts[15]) == $cid && $parts[10] == "verified") {
      $matches[] = $value;
     }
   }
  return $matches;
}

$cid = $_GET['cid'];
$data = file("admin/data/threads.DAT");
$Lines = find_by_colum($data, 3, $cid);
$imgdir = 'admin/thumbs';

print "<table align=\"center\" cellspacing=\"0\">";
print "<tr>";
print "<td><p align=\"center\"><a href=\"?id=view&item=$imagename\"><img src=\"$img\" border=\"0\"><br>$catname</a><br>£$price<br>$add</p></td>\n";

print "</tr>";
print "</table>";
print "<br><br>";

}
}
?>

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/122198-solved-function-stuck-in-loop/
Share on other sites

The problem is that because the line:

 

$Lines = find_by_colum($data, 3, $cid); is in a foreach loop the function is called more than once as the same name. Hence the function cannot be redeclared.

 

I have actually fixed this now with the following:

 

$has_been_run = false;

$bfile = file("admin/data/threads.DAT");
   if($has_been_run == false){
  foreach($bfile as $bKey => $Val){
   $Data[$bKey] = explode("|", $Val);
   	 $has_been_run == true;

   if(trim($Data[$bKey][2]) != trim($_GET['cid']) && trim($Data[$bKey][14]) != trim($_GET['cid']) && trim($Data[$bKey][15]) != trim($_GET['cid']) && trim($Data[$bKey][16]) != trim($_GET['cid']) && trim($Data[$bKey][17]) != trim($_GET['cid'])){
   print "";
   }
   else { 

// do stuff
}
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.