Jump to content

[SOLVED] Problem with Dynamic Drop Down Menu


boney alex

Recommended Posts

Hi, I'm fairly new to PHP and I'm having difficulties displaying the MakeModelID for a row selected from a drop down menu I've populated from a table combining three attributes (Make, Model and Specification - for example Ford Transit LWB 350 HR). What I require is to capture in a variable, such as $MakeModelID, the ID for that record selected. 

 


<fieldset>
<legend>Please select the Type of Van</legend>
	<?php
	$result = mysql_query("SELECT MakeModelID, Make, Model, Specification FROM MakeandModel ORDER BY Make, Model, Specification"); 
	?>
	<label for="branch">Type of Van:</label>
	<select name="vantype" STYLE="width: 220px" size="0">
	<option selected value=""></option> 

	<?php 
	while ($row = mysql_fetch_array($result)) {
	echo "<option value=\"$row[MakeModelID]\">$row[Make] $row[Model] $row[specification]</option>\n";
	} 
	?>
	</select><br>		
</fieldset>

 

Any help would be much appreciated. Thanks! Alex

Complex variables should be surrounded by curly braces when within quotes.

 

echo "<option value=\"{$row['MakeModelID']}\">{$row['Make']} {$row['Model']} {$row['Specification']}</option>\n";

 

Also note that non-numerical array indexes should always be surrounded by quotes.

 

Does that help at all? I'm still note real sure what your question actually was.

do you mind explaining what code I've missed out phoenix?

 

You haven't missed anything except error handling. You should always check your query actually worked before trying to use the result.

 

<fieldset>
<legend>Please select the Type of Van</legend>
	<label for="branch">Type of Van:</label>
	<select name="vantype" STYLE="width: 220px" size="0">
	<option selected value=""></option> 

	<?php
	if ($result = mysql_query("SELECT MakeModelID, Make, Model, Specification FROM MakeandModel ORDER BY Make, Model, Specification")) { 
                  if (mysql_num_rows($result)) {		  
                    while ($row = mysql_fetch_array($result)) {
	      echo "<option value=\"{$row['MakeModelID']}\">{$row['Make']} {$row['Model']} {$row['Specification']}</option>\n";
	    }
                  }
                }
	?>
	</select><br>		
</fieldset>

my code manages to produce the drop down as you can see by visiting:

 

http://co-project.lboro.ac.uk/users/coajs/quoteandbook

 

What I'm strugglin with is once a user selects a van type from the drop down list, how do I capture the MakeModelID for the option selected?

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.