Schlo_50 Posted September 1, 2008 Share Posted September 1, 2008 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 More sharing options...
phpknight Posted September 1, 2008 Share Posted September 1, 2008 Is there a reason that you are defining that function right inside your code? If you just put it outside all that, it should be fine. Link to comment https://forums.phpfreaks.com/topic/122198-solved-function-stuck-in-loop/#findComment-630861 Share on other sites More sharing options...
JasonLewis Posted September 1, 2008 Share Posted September 1, 2008 I don't understand why you would want to be creating a function in a loop anyway. What is the logic behind it? Link to comment https://forums.phpfreaks.com/topic/122198-solved-function-stuck-in-loop/#findComment-630865 Share on other sites More sharing options...
Schlo_50 Posted September 1, 2008 Author Share Posted September 1, 2008 Thats a small error where I was condensing the code for the forum. The function is actually created outside of the loop, and is called from within it. Link to comment https://forums.phpfreaks.com/topic/122198-solved-function-stuck-in-loop/#findComment-630871 Share on other sites More sharing options...
phpknight Posted September 1, 2008 Share Posted September 1, 2008 Something is messed up. I've never had a time where it says I redeclared something I haven't. Sometimes it takes a few hours, but I always find it. Do you have a double include somehow? Try require_once everywhere and see if that fixes it. Link to comment https://forums.phpfreaks.com/topic/122198-solved-function-stuck-in-loop/#findComment-630877 Share on other sites More sharing options...
Schlo_50 Posted September 1, 2008 Author Share Posted September 1, 2008 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 } } } Link to comment https://forums.phpfreaks.com/topic/122198-solved-function-stuck-in-loop/#findComment-630882 Share on other sites More sharing options...
JasonLewis Posted September 1, 2008 Share Posted September 1, 2008 Calling a function more than once isn't re-declaring the function. But then again, I'm confused by the code you posted. ??? Link to comment https://forums.phpfreaks.com/topic/122198-solved-function-stuck-in-loop/#findComment-630884 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.