if I assume that your category table looks like this
+-----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| id | tinyint(4) unsigned | NO | PRI | NULL | |
| parent_id | tinyint(4) unsigned | YES | MUL | NULL | |
| name | varchar(255) | YES | | NULL | |
| note | varchar(254) | YES | | NULL | |
+-----------+---------------------+------+-----+---------+-------+
notice that you have a recursive relationship (parent_id pointing to id)
now, let's populate it with some data like this
+----+-----------+----------------+------+
| id | parent_id | name | note |
+----+-----------+----------------+------+
| 1 | NULL | bakery | NULL |
| 2 | 1 | Cake | NULL |
| 3 | 1 | Bun | NULL |
| 4 | 1 | Toffee | NULL |
| 5 | 1 | Bread | NULL |
| 6 | 2 | Chocolate cake | NULL |
| 7 | 2 | Butter cake | NULL |
| 8 | 3 | Honey bun | NULL |
| 9 | 5 | Italian bread | NULL |
| 10 | 5 | French bread | NULL |
| 11 | 5 | Bereber bread | NULL |
+----+-----------+----------------+------+
so now we create a method in the model that gets all the subcategories of a given id
Model
<?php
class menu_mdl extends CI_Model
{
public function get_subcategories_id($id)
{
$qry_str = "SELECT
categories.id,
categories.`name` AS name
FROM
categories
WHERE
categories.parent_id = ".$id;
$q = $this->db->query($qry_str);
return $q->result();
}
}
?>
then we call this method in the controller menu.php as so
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class menu extends CI_Controller {
public function index()
{
$this->load->model("menu_mdl");
// get all the subcategories of the root category with id = 1
$all_categories_id = $this->menu_mdl->get_subcategories_id(1);
// prepare the menu as an HTML string
$menu = "<ul>";
//loop through level1 then level2 then level3... you can go as many levels as you want!
foreach($all_categories_id as $level1)
{
if($level1->id)
{
$menu .= "<li><a href='#'>".$level1->name."</a></li>";
$sub_level1 = $this->menu_mdl->get_subcategories_id($level1->id);
if ($sub_level1)
{
$menu .= "<ul>";
foreach($sub_level1 as $level2)
{
$menu .= "<li><a href='#'>".$level2->name."</a></li>";
$sub_level2 = $this->menu_mdl->get_subcategories_id($level2->id);
if ($sub_level2)
{
$menu = "<ul>";
foreach($sub_level2 as $level3)
{
$menu .= "<li><a href='#'>".$level3->name."</a></li>";
}
$menu .= "</ul>";
}
}
$menu .= "</ul>";
}
}
}
$menu .= "</ul>";
$data["menu"] = $menu; // <--- this variable has all the menu as HTML string
$this->load->view('menu',$data);
}
}
now in the view we just echo the ready menu by a simple echo like that
view
<?php
echo $menu ;
?>
then the output will look like this
<ul>
<li><a href='#'>Cake</a></li>
<ul>
<li><a href='#'>Chocolate cake</a></li>
<li><a href='#'>Butter cake</a></li>
</ul>
<li><a href='#'>Bun</a></li>
<ul>
<li><a href='#'>Honey bun</a></li>
</ul>
<li><a href='#'>Toffee</a></li>
<li><a href='#'>Bread</a></li>
<ul>
<li><a href='#'>Italian bread</a></li>
<li><a href='#'>French bread</a></li>
<li><a href='#'>Bereber bread</a></li>
</ul>
</ul>
that's all there is to it
update
you can use this controller instead of the previous one, if you want (thanks to Meh for the idea about the recursive function) , to loop through all the subcategories until the last child using a function magix (because it's magic :p )
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class menu extends CI_Controller {
public function index()
{
$this->load->model("menu_mdl");
// get all the subcategories of the root category with id = 1
$all_categories_id = $this->menu_mdl->get_subcategories_id(1);
// prepare the menu as an HTML string
$menu = "<ul>";
//loop through level1 then level2 then level3... you can go as many levels as you want!
foreach($all_categories_id as $level1)
{
// loop until through subcategories until there is no subcategories
$this->magix($level1,$menu);
}
$menu .= "</ul>";
$data["menu"] = $menu; // <--- this variable has all the menu as HTML string
$this->load->view('menu',$data);
}
private function magix($level_id,&$menu)
{
if($level_id->id)
{
$menu .= "<li><a href='#'>".$level_id->name."</a></li>";
$sub_level = $this->menu_mdl->get_subcategories_id($level_id->id);
if ($sub_level)
{
$menu .= "<ul>";
foreach($sub_level as $level2)
{
$menu .= "<li><a href='#'>".$level2->name."</a></li>";
$sub_level2 = $this->menu_mdl->get_subcategories_id($level2->id);
if ($sub_level2)
{
$this->magix($level_id,$menu);
}
}
$menu .= "</ul>";
}
}
}
}
I hope that helps