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

[eluser]shailendra[/eluser]
I tried , but no success. I am getting this error:

Fatal error: Cannot use object of type stdClass as array in C:\Program Files\Apache Group\Apache2\htdocs\codeigniter\application\views\category_view.php on line 6
#12

[eluser]toopay[/eluser]
Code:
<?php
$categories = (array) $categories;
foreach($categories as $category)
{
    // echo $category->category_name;
    $category = (array) $category;
    echo $category['category_name'];
    foreach($category['sub_category'] as $lrow)
    {
            echo $lrow['category_name'];
    }
}
?>
#13

[eluser]shailendra[/eluser]
Now I get the parent category with the code you gave, but I still don't get subcategory.I did some modification to the code you gave as the code you gave was not working for subcategory.

In view:
Code:
$categories = (array) $categories;
foreach($categories as $category)
{
    // echo $category->category_name;
    $category = (array) $category;
    echo $category['category_name'];
    $i=0;
    $category['sub_category'][$i] = (array) $category['sub_category'][$i];
    foreach($category['sub_category'][$i] as $lrow)
    {
        $lrow = (array) $lrow;
        echo $lrow['category_name'];
        $i++;
    }
}

I get following error:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: sub_category

Filename: views/category_view.php

Line Number: 9

A PHP Error was encountered

Severity: Notice

Message: Undefined index: category_name

Filename: views/category_view.php

Line Number: 7

----------------------------------------------------

If I do only this in view:

Code:
<?php
$categories = (array) $categories;
foreach($categories as $category)
{
    // echo $category->category_name;
    $category = (array) $category;
    echo $category['category_name'];
    /*$i=0;
    $category['sub_category'][$i] = (array) $category['sub_category'][$i];
    foreach($category['sub_category'][$i] as $lrow)
    {
        $lrow = (array) $lrow;
        echo $lrow['category_name'];
        $i++;
    }*/
}
?>

I get following error:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: category_name

Filename: views/category_view.php

Line Number: 7
#14

[eluser]shailendra[/eluser]
Please help me on this?
#15

[eluser]broncha[/eluser]
While using foreach() statement you dont need to loop through the numerical indices.

I have made some changes to the code. Hope that will help. I have not tested it but it should work, or you can figure out where it goes wrong as this is how you need to do it

In Model:
Code:
function list_parent_categories()
    {
        $this->db->select('category_id,category_name');
        $this->db->order_by('category');
        $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');
        $this->db->where('category_id',$category_id);
        $this->db->where('active','y');
        $result_product = $this->db->get('category');
        return $result_product->result();
    }

    function no_of_projects($category_id)
    {
        $sql1="select count(proj_id) from projects where parent_id=".$category_id." and status='open'";
        $result_total=$this->db->query($sql)->result();
        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:
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/>";
        }
    }
#16

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

Code:
&lt;?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;

    }
}
?&gt;

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:
&lt;?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;




Theme © iAndrew 2016 - Forum software by © MyBB