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> Quote Link to comment 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.... ?> Quote Link to comment Share on other sites More sharing options...
lc21 Posted July 16, 2007 Author Share Posted July 16, 2007 Thats great, thank you Quote Link to comment 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>"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.