I use this method also, but I usually put a --NONE-- item in at the top of the listbox so the user has the option of selecting none of the items in the list. This may or may not be required for your application.
Also, you can get the select box to automatically have the value that the a given field contains chosen from the list of items by using the 'selected' HTML attribute. By way of an example, the function below will show a list of filenames stored in my database and have the one that I have selected elsewhere in the system automatically shown highlighted in the list. It also shows how to add the '--NONE-- item a thte top of the list: function filelist($file) { $result=mysql_query("SELECT ID,title,type FROM files"); echo "<BR>"; echo "<select name=\"file\"><option value=\"0\">---NONE---</option>"; if (mysql_num_rows($result)==0) { //no files } else { while ($row=mysql_fetch_array($result)) { $ID=$row["ID"]; $title=$row["title"]; $type=$row["type"]; if ($row["ID"]==$file) { echo "<option selected value=\"$ID\">" . $type . " - " . $title . "</option>"; } else { echo "<option value=\"$ID\">" . $type . " - " . $title . "</option>"; } } } echo "</select><BR>"; } Note that unless one of the list items has the 'selected' attribute set, then none of the items in the list will be shown as the "chosen one" in the list - it will just be blank when the user opens the page. Cheers Andrew. ----- Original Message ----- From: "David Rodman" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Saturday, June 26, 2004 6:44 AM Subject: Populating a <SELECT> from a database > Here's a way to do it with PHP: > > function field_select($table, $field) > { $result = mysql_query("SELECT $field FROM $table") or > die(mysql_error()); > print "<SELECT NAME=\"" . $field . "\">\n"; > $limit = mysql_num_rows($result); > for($i = 0; $i < $limit; ++$i) > { list($value) = mysql_fetch_array($result); > print "<OPTION VALUE=\"" . $value . "\">$value</OPTION>\n"; > } > print "</SELECT>\n"; > } > > > > -- > MySQL General Mailing List > For list archives: http://lists.mysql.com/mysql > To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED] > > -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]