954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with foreach when using linking

I am trying to use the command foreach to create a drop down menu. That I can usually do, the problem is I am also adding some linking. Here is some my code:
<?sql

$sql2 = "SELECT mn.map, mn.route FROM maps m JOIN map_name mn ON (m.map_name = mn.map) WHERE `ship`='".$POST['ship']."'";
$result2 = mysql_query($sql2)
or die("Query Error".mysql_error());
while ($row = mysql_fetch_array($result2)) {
$mapname[$row['mn.map']] = $row['mn.route'];
}
?>



Map :


Select a Map
<?php
foreach( $mapname as $map => $route ){ **
?>
<?php echo $route ?>
<?php }
?>



** I get the error Invalid argument supplied for foreach() for this line.

I've tried quite a few different things for this, and I now feel like I am beating my head against the wall. Thanks for the help.

samergamer
Newbie Poster
1 post since Jun 2004
Reputation Points: 10
Solved Threads: 0
 

Sounds like that your query is pulling nothing. What I would do, is above the while() section of your code, add [php]$mapname = array();[/php] This will force $mapname to be an array - otherwise, if your query pulls nothing, then the while() block won't be interpreted and hence your mapname is undefined. So when foreach() asks for it, it finds that its not an array and starts whining. Forcing $mapname to be an array will just mean that the foreach doesn't do anything if its empty.
Obviously, you need to put this code above wherever else that you may have used $mapname as it will destroy it first.
I do, however, have some concerns about your query.

`ship`='".$POST['ship']."'";


Should ship, not in fact be, m.ship or mn.ship?

Roberdin
Supreme Evil Overlord
Team Colleague
282 posts since Feb 2003
Reputation Points: 63
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You