Hello:
I have a table with three columns. They are:
category_id PK auto_increment
category_name
parent_id
I need to sort that data so sub-categories appear underneath the correct category.
The data looks like this:
category_id category_name parent_id
1 Hardware 0
2 Software 0
3 DVD Movies 0
4 Printers 1
5 Memory 1
6 Drama 3
7 Sci Fi 3
8 Simulation 2
9 Educational 2
The root category is parent_id = 0.
The subcategories is parent_id = category_id.
The category list should appear like this:
Hardware
- Printers
- Memory
Software
- Simulation
- Educational
DVD Movies
- Drama
- Sci Fi
You can see that the categories with parent_id = 1 is the same as category_id =1.
I am having a hard time creating the category list to appear like the one above.
Can someone help me out?
Thanks in advance.
This is my script:
Code: Select all
<?php
$conn = mysql_connect("localhost", "root", "xxx");
mysql_select_db("cart",$conn);
$query = "SELECT * FROM category WHERE parent_id = category_id";
$result = mysql_query($query, $conn) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$category_id = $row['category_id'];
$category_name = $row['category_name'];
$parent_id = $row['parent_id'];
echo $category_id;
echo $category_name;
echo $parent_id;
}
?>Thanks again for the help.