Category Structure

Hey guys, I have a minor aesthetics problem that is driving me nuts. I have a table in a database that is structured as follows:
categories
id
name
description
parent_id
date

I have a dropdown that is pulling all these value dynamically, and here is my problem. Some of the categories are sub-categories, and I would like for them to display after their parent category. However, right now it just pulls everything as it shows up int he database. How do I display the list as I would like it?

Example ([id:1]Cars->[id:5]Honda) shows up on the list as:
Cars
-Honda

instead of:
Cars
something
something
something
-Honda

I think you’re looking for something like this:

function echoOptions($parentid = 0) {
  $sql = "SELECT id, name FROM categories WHERE parent_id = ".$parentid." ORDER BY name DESC";
  $data = mysql_query($sql);
  while ($catinfo = mysql_fetch_assoc($data))
    echo "<option value='".$catinfo['id']."'>".$catinfo['name']."</option>";
    echoOptions($catinfo['id']);
  }
}

Something like that should work.

It doesn’t seem to work. I copied what you had and placed into the page, but nothing seems to display currently. I’m still looking things over to try and find out why.

Sponsor our Newsletter | Privacy Policy | Terms of Service