Ryokotsusai Posted January 10, 2008 Share Posted January 10, 2008 Hi, I have a small function that is supposed to retrieve a list of links from a database and then build the link list from what it retrieves, but for some reason I cannot get it to work. It errors out on the "foreach" line saying: Warning: Invalid argument supplied for foreach() in /home/revrocom/public_html/character/cnt/cnf/lnk.php on line 52 but when i printed $lin it contained the array The actual page can be found here and here is the function where it errors followed by what the print_r outputs: function links($area,$tabs = 0){ global $LINK; $i = 0; $f = count($LINK[$area]); // = 6 $t = " "; if($tabs > 0) { while($tabs > 0) { $tab .= $t; $tabs--; } } else { $tab = ""; } while($i <= $f) { $end = $tab."<li><a "; $lin = $LINK[$area][$i]; foreach($lin as $key => $val) { // Line 52 $end .= ($val != "" && $val != NULL && $key != 'text') ? $key."=\"".$val."\" " : ""; $end .= ($val != "" && $val != NULL && $key == 'text') ? ">".$val."</a></li>" : ""; } print_r($lin); $i++; unset($lin); } echo $end; } /* print_r output: Array ( [href] => http://char.rev-ro.com/cnt/cp/home/ [title] => Home Page [id] => [class] => [text] => Home ) Array ( [href] => http://char.rev-ro.com/cnt/cp/user/ [title] => User Administration [id] => [class] => [text] => User Control ) Array ( [href] => http://char.rev-ro.com/cnt/cp/admin/ [title] => Administrative Control [id] => [class] => [text] => Admin Control ) Array ( [href] => http://char.rev-ro.com/cnt/cp/site/ [title] => Site Control [id] => [class] => [text] => Site Control ) Array ( [href] => http://char.rev-ro.com/cnt/cp/stats/ [title] => Server Statistics [id] => [class] => [text] => Stats ) Array ( [href] => http://char.rev-ro.com/cnt/cp/help/ [title] => Help / Contact Us [id] => [class] => [text] => About ) */ Thanks Link to comment https://forums.phpfreaks.com/topic/85283-solved-foreach-invalid-argument/ Share on other sites More sharing options...
kenrbnsn Posted January 10, 2008 Share Posted January 10, 2008 Put the print_r before the foreach. Also print out the value of $i, so you know where it is stopping. Ken Link to comment https://forums.phpfreaks.com/topic/85283-solved-foreach-invalid-argument/#findComment-435125 Share on other sites More sharing options...
Ryokotsusai Posted January 10, 2008 Author Share Posted January 10, 2008 ah, oops now I feel special: while($i <= $f) { loops through it an extra time should have been while($i < $f) { Thank you Link to comment https://forums.phpfreaks.com/topic/85283-solved-foreach-invalid-argument/#findComment-435130 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.