I am using the script to populate drop down boxes in a form, however I am using it four times, the first box is fine, but 2, 3 & 4, Do not give me the Select Category as the first option, it gives me the last of the options so the user would need to change them to get them to not show at all. I am selecting them from the same database, and they are given four options. What am I doing wrong.
Here is the script
<?php
mysql_connect("","","");
mysql_select_db("");
$query1_result = mysql_query ("SELECT category FROM category");
$num_results = mysql_num_rows($query1_result );
$categories = array();
//use the for loop to populate the array with the unitnos from the query
for ($i=0; $i<$num_results; $i++)
{
$category_row=mysql_fetch_object($query1_result );
$categories[]=$category_row->category;
}
//start the dropdown box
echo "<select name=\"category3\" value=\"$category\" >";
//detect if this script has been submited yet
if (!isset ($category))
{
$selected_category = "";
}
else
{
$selected_category = $category;
}
if ($selected_category == "")
{
echo "<option value=\"\" SELECTED>Select Category</option>";
}
else
{
echo "<option value=\"\" >Select Category</option>";
}
//populate the option and provide a SELECTED option for each item in the array
foreach ($categories as $category)
{
if ($selected_category == $category)
{
echo "<option value=\"$category\" SELECTED >$category</option>";
}
else
{
echo "<option value=\"$category\" >$category</option>";
}
}
echo "</select>";
?>
Thanks in advance.