A common question on the PHP mailing list is "How do I populate a select list with items in a database?". Thank the maker for the while construct, because it makes this all very easy!
In Chapter 7, "Advanced PHP Techniques: Web-Based Database Adminstration" of PHP Essentials, I use very verbose coding techniques to match, then print, an <OPTION> field within a <SELECT> form element (drop-down list).
I'm just going to dive right in here, and show you how to use a while loop to get a list from a database table. If you are having trouble following the concept, blame it on me for now explaining it thoroughly, and contact me to help fill in the gaps.
Suppose you have a database table called OPERATING_SYSTEMS, which contains one field (OS) and looks something like this:
+-------------------+
| OS |
+-------------------+
| Win 95/98 |
| Linux (SuSE) |
| Linux (Red Hat) |
| Win NT |
| Linux (Mandrake) |
| Solaris |
| Linux (Debian) |
| Win 2000 |
| FreeBSD |
| Linux (Other) |
| Mac OS |
| Irix |
| Linux (Caldera) |
| Linux (Slackware) |
+-------------------+
The goal is to bring the contents of the OPERATING_SYSTEMS table into some sort of form you're making, so you would do the following with MySQL (you can substitute any database connectivity code that you would normally use).
First, start an HTML shell and add your basic database connectivity code, including the SQL statement:
// create SQL statement
$sql = "SELECT OS FROM OPERATING_SYSTEMS
ORDER BY OS ASC";
// execute SQL query and get result
$sql_result = mysql_query($sql,$connection)
or die("Couldn't execute query.");
?>
Next, since the values will be represented within the context of a form, pretend that you have a form. I've made a fake one, below. In a few minutes, we'll create a string called $option_block, which will contain all of the <OPTION> variables created by sucking data out of the MySQL table (OPERATING_SYSTEMS). That's why the echo statement appears in the code below.
Now go back into your PHP code, and add the while loop that will get the list of operating systems and put them in <OPTION> elements. First, separate the results by row, using the mysql_fetch_array function:
while ($row = mysql_fetch_array($sql_result)) {
// more code here
}
The while loop will create an array called $row for each record in the result set. To get the individual elements of the record (in this case, simply OS), create specific variables:
$os = $row["OS"];
Put the value inside an <OPTION> element. Remember, we'll be adding to the $option_block string as the loop continues, so use the concatenation operator (.=) instead of the assignment operator:
Using the code above, the loop will print a drop-down list box and submit button like the following:
Suppose, however, that this form was part of a "Modify Record" script, where you were pulling a record's values from a database table, and pre-populating a form with the values of that record. One simple modification to the above code will help you to pre-select an item in the drop-down list box.
If the record contains a value called $curr_os, and you want to pre-select an item in the drop-down list box based on that values, simply re-write the while loop like this:
Using this new code, and supposing that the value of $curr_os was "Solaris", the loop will print a drop-down list box and submit button like the following:
Anonymous wrote :30 I have tried this solution in a little bit different way, but i
dont get it to work. Im going to use the "output" of the
<select>, to insert to a different table in the same
database. Which means that i have two queries in the
same script, one "insert into table1..! and one select *
from table2.." How can i combine these two, so i can
use info from some tables, and then put the info into a
different table, with the drop down box?
Anonymous wrote :76 For a long time now i have tried to get this example to
work. With a few minor changes i got it to work :) Here
is what i did:
// $os = $row["OS"]; and altered
$option_block .= "<OPTION
value=\"$os\">$os</OPTION>";
to $option_block .= "<OPTION value=`".$row
[0]."`>".$row[0]."</OPTION>";
Now it works - I cant say whats wrong in the example,
but this works for me :)