lc21 Posted July 16, 2007 Share Posted July 16, 2007 Hi, sorry for a novice question but in PHP how do you populate a list dynamically? for example I have the following in a table but I can't get them into the list after the query is performed. <form action=""> <select name="cars"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="fiat">Fiat</option> <option value="audi">Audi</option> </select> Link to comment https://forums.phpfreaks.com/topic/60202-solved-dynamic-list-population/ Share on other sites More sharing options...
ToonMariner Posted July 16, 2007 Share Posted July 16, 2007 if you have these items listed in a database table then this would work... <pre> id name ------------- 1 Volvo 2 Saab 3 Fiat 4 Audi <?php $qry = "SELECT * FROM `cars`"; $qry = mysql_query($qry); if (mysql_num_rows($qry) > 0) { ?> <select name="cars"> <?php while($row = mysql_fetch_assoc($qry)) { ?> <option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option> <?php } ?> </select> <?php ...rest of script.... ?> Link to comment https://forums.phpfreaks.com/topic/60202-solved-dynamic-list-population/#findComment-299469 Share on other sites More sharing options...
lc21 Posted July 16, 2007 Author Share Posted July 16, 2007 Thats great, thank you Link to comment https://forums.phpfreaks.com/topic/60202-solved-dynamic-list-population/#findComment-299470 Share on other sites More sharing options...
jvrothjr Posted July 16, 2007 Share Posted July 16, 2007 Where is a function is use to do this Pass the table use wish to query and the field that holds that data Function Queryselectdisplay($tablename,$fieldname) { $QSdist = "select Distinct ".$Fieldname." from ".$tablename." ORDER by ".$fieldname." ASC"; echo "<select name=".$fieldname.">"; echo "<option echo value=''>Make Selection</option>"; $options=mysql_query($QSdist); while($data = mysql_fetch_array($options)) { echo "<option value='".$data[$fieldname]."'>".$data[$feildname]."</option>"; } echo "</select>"; } Link to comment https://forums.phpfreaks.com/topic/60202-solved-dynamic-list-population/#findComment-299473 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.