Introduction:
Here I am going to create a three level category system using PHP and mysql. For
example Category -> Sub Category -> Sub of sub category.
Description
Step 1
Create a database table having these column name id, name, parent .Here
parent shows category and subcategory .Example if parent=0 then it is the
root and if parent=1 it has a category of value having id 1.
I had included database file db.php.
Step 2
Dump values in the table. Insert the values in the columns id, name and their
parent using my sql query.
Step 3
Add the php script including stylesheet.
<html>
<head>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body bgcolor=" #98AFC7">
<br>
<h2 align="center"><ul>Three Level Category</ul> </h2>
<br>
<?php include "db.php";
//$conn=mysql_connect("localhost","root","ost");
//mysql_select_db("product_category",$conn);
$category = mysql_query( "Select * FROM categories
where parent=0", $conn);
if(!$category)
{
die( "Data Base Query1 Failed: " . mysql_error());
}
while($product=mysql_fetch_array($category))
{
?>
<ul id="nav">
<?php echo '<li class="parent"><a href="#" >'.$product[name].'</a>';?>
<ul>
<?php
$subcategory_set = mysql_query( "Select * FROM
categories where parent=$product[id]", $conn);
while($childproduct=mysql_fetch_array($subcategory_set))
{
echo '<li class="parent"><a href="#" >'.$childproduct[name].'</a>';
?>
<ul>
<?php
$subsubcategory_set = mysql_query(
"Select * FROM categories where parent=$childproduct[id]", $conn);
while ($childchildproduct=mysql_fetch_array($subsubcategory_set))
echo '<li><a href="#" >'.$childchildproduct[name].'</a></li>';
?>
</ul>
<?php } ?>
</li>
</ul>
</ul>
<?php } ?>
</body>
</html>