Hi,
I'm struggling with creating a dynamic drop down menu and hope someone can help with some direction, or possibly even a code snippet to build on.
I have a table called 'cat_table'
fields are cat_id, cat_name, cat_parent, create_date, last_update
What I am creating is an ad page, displayed by category / sub-category
Members can submit their ads and select whatever category / sub they want it listed under, and if there isn't an appropriate cat or sub-cat, they will be able to add one.
But, what I want is to create the drop down from a mysql query, and list all the cats alphabetically, and then under each cat if there are any sub cats, list them under each primary category.
What I would really like to do is have unlimited levels of sub categories, but that isn't that important right now... Just need to get the basics up now.
Here is what I have to display the main categories:
and what I was thinking was maybe I needed to call a function to get the subs, if there are any that exist, which would require another query for every primary category that is listed...
$sql="SELECT cat_id, cat_name FROM cat_table where cat_parent='0' ORDER BY cat_name asc";
//print "<br />sql is <br />".$sql."<br />";
$base_cat = mysql_query($sql) or die($sql);
$cats=0;
print"<select name=\"categories\">";
while($cat_list=mysql_fetch_array($base_cat)){
++$cats;
print "<option value='".$cat_list[0]."'>".$cat_list[1]."</option>";
//sub_cat($cat_list[0]); // this will be a call to a function for sub cats
}
print'</select>';
Any suggestions would be greatly appreciated.
Thanks in advance for any assistance you may be able to provide.