Jump to content

[SOLVED] foreach, invalid argument?


Ryokotsusai

Recommended Posts

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

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.