Welcome Guest, Not a member yet? Register   Sign In
Display Nested categories
#16

[eluser]shailendra[/eluser]
Thanks a lot Broncha.......it worked :-) . I had to do slight modification in Model:

Code:
<?php
class Category_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function list_parent_categories()
    {
        $this->db->select('category_id,category_name');
        $this->db->order_by('category_name');
        $this->db->where('parent_id',0);
        $this->db->where('active','y');
        $result_product = $this->db->get('category');
        return $result_product->result();
    }

    function list_sub_categories($category_id)
    {
        $this->db->select('category_id,category_name');
        $this->db->order_by('category_name');
        $this->db->where('parent_id',$category_id);
        $this->db->where('active','y');
        $result_product = $this->db->get('category');
        return $result_product->result();
    }

    function no_of_projects($category_id)
    {
        $sql="select count(proj_id) as total from projects where category_id=".$category_id;
        $result_total=$this->db->query($sql);
        if($result_total->num_rows()>0){
            $row   = $result_total->row();
            $count   =  $row->total;
        }
        return $count;

    }
}
?>

In Controller
Code:
function index()
    {
       $this->load->model('category_model');
       $data = array();
       $categories = array();
       $categories = $this->category_model->list_parent_categories();
      
       foreach($categories as $parent_cat)
       {
          $parent_cat->sub_category =  $this->category_model->list_sub_categories($parent_cat->category_id);
          $parent_cat->total_projects = 0;
          
          foreach($parent_cat->sub_category as $sub_cat)
          {
            $sub_cat->proj_counts = $this->category_model->no_of_projects($sub_cat->category_id);
            
            $parent_cat->total_projects += $sub_cat->proj_counts;
          }
       }
    
       // Check the value
       //var_dump($categories);
       // If above line works fine, just assign it into the view
        $data['categories'] = $categories;
        $this->load->view('category_view', $data);
    }

In view
Code:
<?php
foreach($categories as $parent_cat){
        echo $parent_cat->category_name."( ".$parent_cat->total_projects." )"."<br/>";
        foreach ($parent_cat->sub_category as $sub_cat){
                echo "  -".$sub_cat->category_name."( ".$sub_cat->proj_counts." )"."<br/>";
        }
    }
?&gt;


Messages In This Thread
Display Nested categories - by El Forum - 07-10-2011, 10:06 PM
Display Nested categories - by El Forum - 07-14-2011, 04:11 AM
Display Nested categories - by El Forum - 07-14-2011, 04:20 AM
Display Nested categories - by El Forum - 07-15-2011, 12:18 AM
Display Nested categories - by El Forum - 07-15-2011, 02:00 AM
Display Nested categories - by El Forum - 07-17-2011, 11:31 PM
Display Nested categories - by El Forum - 07-17-2011, 11:39 PM
Display Nested categories - by El Forum - 07-18-2011, 03:19 AM
Display Nested categories - by El Forum - 07-18-2011, 05:51 AM
Display Nested categories - by El Forum - 07-18-2011, 09:47 AM
Display Nested categories - by El Forum - 07-18-2011, 11:20 PM
Display Nested categories - by El Forum - 07-18-2011, 11:24 PM
Display Nested categories - by El Forum - 07-19-2011, 01:30 AM
Display Nested categories - by El Forum - 07-20-2011, 09:25 PM
Display Nested categories - by El Forum - 07-20-2011, 11:06 PM
Display Nested categories - by El Forum - 07-21-2011, 03:34 AM



Theme © iAndrew 2016 - Forum software by © MyBB