Reputation: 3548
i have MySql Table name 'category', in that table i have
id catname parent_id
1 animals
2 vegs
3 dog 1
4 cat 1
5 carrot 2
i just wanna display this data in html nested 'ul' like
<ul>
<li>Animals
<ul>
<li>dog</li>
<li>cat</li>
</ul>
</li>
<li>Vegs
<ul>
<li>Carrot</li>
</ul>
</li>
</ul>
with php, please help me to get this data with php(CodeIgniter) and display.
Upvotes: 1
Views: 3035
Reputation: 84
Here are the three components for your question: controller, model and view...
<!-- THE VIEW -->
<ul>
<?php foreach($main_cat->result() as $rowm) : ?>
<li><?php echo $rowm->catname ?>
<ul>
<?php
$id = $rowm->id;
$sec_cat = $this->yourmodel->get_secondary($id);
foreach($sec_cat->result() as $rows) :
?>
<li><?php echo $rows->catname ?></li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
<!-- THE CONTROLLER -->
<?php
class Welcome extends CI_Controller {
function index()
{
$data['main_cat'] = $this->yourmodel->get_main();
$this->load->view('welcome_view',$data);
}
}
?>
<!-- THE MODEL -->
<?php
class Yourmodel extends CI_Model {
function get_main()
{
$this->db->where('parent_id','');
$query = $this->db->get('category');
return $query;
}
function get_secondary($parent)
{
$this->db->where('parent_id',$parent);
$query = $this->db->get('category');
return $query;
}
}
?>
Upvotes: 1