PDA

View Full Version : dynamic list boxes populated from MYSQL dbase


fitchic77
09-13-2002, 11:57 PM
I really need an example on how to do this....I'm stumped.

Any kind of help would be greatly appreciated!!

Thanks a million
Andrea
:D

Ökii
09-14-2002, 08:42 AM
Assumes passed variable values for
$listbox - the list to use
$chozen - an already chozen list value.

$que = "SELECT * FROM `tablename` WHERE listbox='$listbox'";
echo '<select name="mylist" style=".....">
<option value="">Select an Option Below</option>
';
while($res = mysql_fetch_row($que)) {
echo '<option value="'.$res["optionvalue"].'"';
if($res["optionvalue"] == $chozen) {
echo ' selected';
}
echo '>'.$res["optionname"].'</option>';
}
echo '</select>';

That should query `tablename` and return result rows where the 'listbox' field equals the predetermined value of $listbox.
You could build an array from the returns and run a quick sort($array) on it if you wanted them alphabetical. Anyway, that should be enough for you to start playing with.

downsize
09-14-2002, 10:29 AM
if I may...
ORDER BY ASC or DESC to provide a sort and save some php code and server resources. let mysql do it, it is optimized for that type of sort and since you are already making the db call.. why not? :)

fitchic77
09-14-2002, 01:30 PM
Thanks a million.

Just a quick question (since I'm knew). Where shoudd this code get placed. I know it needs to be placed where I need the item on the form to show up, but is that good programming etiquette.

Thanks again.
Andrea