Hi all...
This is the craziest thing, and I've honestly been seeking out the answer to this for collectively 8+ hours, and can't really afford to put this much time into what should be a simple matter.
Here is the situation:
I have an ad directory for members to post their ads in, based on category and/or sub-category.
When they want to update their ad, it is retrieved from the mysql db, along with the current category that it is listed in ($cur_cat).
But the problem is that their previously selected category doesn't appear as 'selected' in the drop down box, so if they update without changing the category to the one they want, it is updated as the first selection in the dropdown box.
I have tried numerous ways to write the code, and searched and searched for the solution, but it is driving me nuts...
Here is the code block where the issue arises, and coming into this I have $cur_cat set at '6' for purposes of testing.
<?php
// Get listing of base categories
$sql="SELECT cat_id, cat_name FROM cat_table where cat_parent='0' ORDER BY cat_name asc";
$basecat = mysql_query($sql) or die($sql);
print"<select name=\"category\">";
while($cat_list=mysql_fetch_array($basecat)){
if ($cat_list[0]==$cur_cat){$test= ' selected';}else{$test='';}
print '<option value="'.$cat_list[0].'"'.$test.'>'.$cat_list[1].'</option>';
sub_cat($cat_list[0]);// this calls the function for sub cats
}
print'</select>';
// ***********************************************************************
// Sub-Category Function
function sub_cat($cat_num){
$subcat = mysql_query("SELECT cat_id, cat_name FROM cat_table where cat_parent='$cat_num' ORDER BY cat_name asc");
while($sub_cat_list = mysql_fetch_array($subcat)){
if ($sub_cat_list[0]==$cur_cat){$test=' selected';}else{$test='';}
print '<option value="'.$sub_cat_list[0].'"'.$test.'> '.$sub_cat_list[1].'</option>';
}
}
// ***********************************************************************
?>
The resulting html is as follows and option 6 should say 'selected' but it doesn't.
<select name="category">
<option value="4">Advertising</option>
<option value="8"> Newspaper</option>
<option value="10"> Radio</option>
<option value="9"> Television</option>
<option value="2">Autos</option>
<option value="5"> Chevy</option>
<option value="7"> Dodge</option>
<option value="6"> Ford</option>
<option value="3">Marketing</option>
</select>
If anybody has any ideas why this isn't working, or what I can do to get through this, I would seriously appreciate it.
Douglas