•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,189 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,819 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2011 | Replies: 1
![]() |
•
•
Join Date: Jun 2004
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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'];
}
?>
<form action="edit_map_which.php" method="post">
<table border=0 cellspacing=1>
<tr>
<td>
Map :
</td>
<td>
<SELECT name="map">
<option value="" SELECTED>Select a Map</option>
<?php
foreach( $mapname as $map => $route ){ **
?>
<option value="<?php echo $map ?>"><?php echo $route ?></option>
<?php }
?>
</SELECT>
</td>
</tr>
<tr>
<td>
<INPUT type="submit" name="Submit" value="Submit">
</td>
</tr>
</table>
** 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.
<?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'];
}
?>
<form action="edit_map_which.php" method="post">
<table border=0 cellspacing=1>
<tr>
<td>
Map :
</td>
<td>
<SELECT name="map">
<option value="" SELECTED>Select a Map</option>
<?php
foreach( $mapname as $map => $route ){ **
?>
<option value="<?php echo $map ?>"><?php echo $route ?></option>
<?php }
?>
</SELECT>
</td>
</tr>
<tr>
<td>
<INPUT type="submit" name="Submit" value="Submit">
</td>
</tr>
</table>
** 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.
•
•
Join Date: Feb 2003
Location: London, England
Posts: 281
Reputation:
Rep Power: 7
Solved Threads: 6
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. Should ship, not in fact be, m.ship or mn.ship?
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']."'";
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- HELP!! Please review my new website. (Website Reviews)
- Plz help me with the Errors Pasted in the Thread. (C++)
- gcc on windows-linking problem (C++)
- Help! error LNK2001 & error LNK (C++)
- simple checkbox form array help! (PHP)
- C++ linking problem (C++)
- strtok_r() error on solaris 8 (C)
- Possible Airport Problem? (Apple Hardware)
Other Threads in the PHP Forum
- Previous Thread: I need help with a parse error!
- Next Thread: Automatically writing webpages by submitting a form...


Linear Mode