I have a problem with my code. What is the best way to display all categories with n-subcategories?
My MySQL database looks like:
CREATE TABLE categories (
category_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
parent_id INT UNSIGNED NOT NULL DEFAULT 0,
name TEXT NOT NULL,
user INT NOT NULL,
PRIMARY KEY (category_id),
INDEX parent (parent_id)
);
Where user
is id from another table (if category have user = '2'
, user with id = '2'
can see that category)
category_id parent_id name user
1 0 Main category 1 2
2 1 Subcategory 1 2
3 1 Subcategory 2 2
4 3 SubSubcategory 1 2
My php code:
<?php
include('config.php'); //Mysql connect file
//get id from user
if($_GET['id'] != $user_id) {
$id = $_GET['id'];
//check to make sure the user is an admin, who can change mysql
$result = mysql_query("SELECT * FROM users WHERE username = '$username' AND admin = '1'");
$rowCheck = mysql_result($result, 0);
//if the query returns a number, we know the user is an admin. And here we can VIEW our categories
if($rowCheck > 0) {
$user_result = mysql_query("SELECT * FROM users, categories WHERE id = '$id'");
while($row = mysql_fetch_array($user_result)) { ?>
<ol>
<li><a href=""><?php echo $row['name']; ?></a></li>
</ol>
<?php }
} else {
echo "You are not admin";
}
//bla bla bla etc.
...
}
How can I view categories like
<ol>
<li>
<a href="">Main category 1</a>
<ol>
<li><a href="">Subcategory 1</a></li>
<li><a href="">Subcategory 2</a>
<ol>
<li><a href="">Subsubcategory 1</a></li>
</ol>
</li>
</ol>
</li>
</ol>