Tutorial : Recieving data (from MySQL) and displaying in a combo (drop down list box).
Level : Intermediate

This is very basic tutorial, actually someone requested me through PM but I didnt have time to repond him quickly, so here it is now.I assume you already know how to insert data into tables.or read the following tutorial before reading this

Creating table with MySQL and PHP
Retreiving data from MySQL table

Ok - I assume you have already created a table called category..
you have added a few categories into this table, e.g swishmax, swish 2, flash mx and photoshop.
the field name is cat_name which we will use in next step to fetch data.
 
here is the quickest way.

echo’<select name=”categories”>’;

$res=mysql_query(”select * from category”);
if(mysql_num_rows($res)==0) echo “there is no data in table..”;
else
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
echo”<option>$row[cat_name]</option>”;
}
echo’</select>’;

Description of line echo’<select name=”categories”>’;
this is simple text for creating a combo in html, we give it a name becuase we will have to use this name again when any category is selected, for example someone has seelcted the flash mx category, then the variable categories will have a value flash mx..

Description of line $res=mysql_query(”select * from category”);
this is simple query for fetching all fields form table. But you can use different if you dont want to fetch all fields, you may use WHERE clause to make it more specific.

Descrition of if(mysql_num_rows($res)==0) echo “there is no data in table..”;
this will simply tell if there is any data in the table or the table is empty.

for() loop will simply fetch all the categories added into table and display.

echo’</select>’; is simply closing tag for the combo box.

I hope it will help someone to figure how is a dyanmic combo box is created while recieveing data from MySQL