View Full Version : How to show php variable in Select Box

11-03-2006, 10:18 AM
I have an html comboBox for batchyear on a php page.
The batchyear is read from MySql database and saved as php variable
$batchyear . Now i want to set the selected value of this <Select> comboBox as $batchyear.
How to do this?

Thanks in advance!

11-03-2006, 11:26 AM
Quite simple, just echo the values out and construct a select tag and its options.

Say you have constructed an array of the data you have pulled from the database. than you can do the following

for( $i = 0; $i < count( $array_of_data); $i++ )
echo '<option value=" ' . $array_of_data[ $i ] . ' "> ' . $array_of_data[ $i ] . '</option>';
} # ends for loop

don't forget to start the process of building your select tag before the for loop and close it once the for loop is complete.

good luck