• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display Nested categories

#1
[eluser]shailendra[/eluser]
I want to display various project categories and no. of projects posted for each catgory. For example
Web & Programming(2)
-PHP(1)
-ASP(1)
Graphic Designing(2)
-Logo(2)
-Website Design(0)

I did this in php using following code. How do I do this in codeigniter?

Code:
<?php
    $category_details_model = new CategoryDetailsModel();
    $category_details_array = array();
    $category_details_array= $category_details_model->displayparentcategory(0);
    for($i=0;$i<count($category_details_array);$i++) {
        if(($category_details_model->noofactiveprojects($category_details_array[$i]->category_id,'public'))){
?&gt;
      <tr>
        <td height="20"> - <a >category_id?&gt;">&lt;?php echo $category_details_array[$i]->category_name ?&gt;</a>(&lt;?php echo $category_details_model->noofactiveprojects($category_details_array[$i]->category_id,'public') ?&gt;)</a></td>
      </tr>
&lt;?php
        }
}
?&gt;


Function to list all parent category


Code:
/* Function to list all parent category*/
public function displayparentcategory($parent_id){
  
    $query=sprintf("select category_id,category_name from category where parent_id=%d and active='y' order by category_name asc;",
    mysql_real_escape_string($parent_id));    
    //echo $query;

    $category_array  = array();
    $result   = mysql_query($query);
    $num_rows = mysql_num_rows($result);
    if($num_rows!=0) {
      while ($row = mysql_fetch_assoc($result)) {
        $category_details = new CategoryDetails;
        $category_details->category_id  = $row['category_id'];
        $category_details->category_name = $row['category_name'];
        //echo $category_details->category_name;
        $category_details->parent_id = $row['parent_id'];
        //echo $bid_details->proj_name;
        $category_details->date_time = $row['date_time'];
        //echo $bid_details->proj_name;
        $category_array[]=$category_details;

      }
      return $category_array;
    } else {
      return NULL;
    }
  }


Display all projects in a parent category

Code:
//Display all projects in a parent category
public function noofactiveprojects($category_id,$visibility){
            $query1= sprintf("select category_id from category where parent_id=%d;",mysql_real_escape_string($category_id));
            //echo $query1 ."\n";
            $result1 = mysql_query($query1);
            $total_num_proj=0;
            while($row1 = mysql_fetch_array($result1)){
                $category_id=$row1['category_id'];
                if($visibility=="all"){
                    $query2= sprintf("select * from projects where category_id=%d and status='open';",
                    mysql_real_escape_string($category_id));
                }
                else{
                    $query2= sprintf("select * from projects where category_id=%d and status='open' and job_visibility='%s';",
                    mysql_real_escape_string($category_id),
                    mysql_real_escape_string($visibility));
                }
                //echo $query2;
                $result2=mysql_query($query2);
                $num_proj=1;
                while($row2=mysql_fetch_array($result2)){
                    $projectposteddate=date('d-m-Y',strtotime($row2['date_time']));
                    $noofdays="+".$row2['job_display_days']." day";
                    $displayenddate=strtotime ( $noofdays,strtotime($projectposteddate));
                    //echo "displayenddate ".date('d-m-Y',$displayenddate);
                    $todaysdate=date("d-m-Y");
                    $difference=$displayenddate-strtotime($todaysdate);
                    $daysleft=round($difference / 86400);
                    if($daysleft>0){
                        $total_num_proj=$total_num_proj+$num_proj;
                    }  
                }
            }
            return $total_num_proj;
  }

#2
[eluser]shailendra[/eluser]
Can't anyone help me on above problem?

#3
[eluser]toopay[/eluser]
First, you need to move your database abstraction into a separated class : read CI Model Spec to do that. Once you've done with that, create a Controller to handle the request, and when you need to interact with your database, call your model class. In CI, you do
Code:
// This is actually instantiate a model class
$this->load->model('bar_model');
$foo = $this->bar_model->some_db_function();
instead what you done before
Code:
// This probably still works, if you include the file 'bar_model.php' before this line, but it is not the way CI works.
$bar = new bar_model;
$foo = $bar->some_db_function();

#4
[eluser]shailendra[/eluser]
Basically, I want to know how to pass data from 3 different model functions to a view(via controller) and access it using foreach loop?

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_array();
    }

    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_array();
    }

    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;

    }

I want to call above functions in a controller and pass the data to a single view so that I get this output
Web & Programming(2)
-PHP(1)
-ASP(1)
Graphic Designing(2)
-Logo(2)
-Website Design(0)

Please help?

#5
[eluser]toopay[/eluser]
In controller...
Code:
public function index()
{
   $this->load->model('model_name');
   $data = array();
   $categories = array();
   $categories = $this->model_name->list_parent_categories();
   $i = 0;
   foreach($categories as $category)
   {
      $categories[$i]['total_project'] =  $this->model_name->no_of_projects($category['category_id']);
      $i++;
   }
   // Check the value
   var_dump($categories);
   // If above line works fine, just assign it into the view
   // $data['categories'] = $categories;
   // $this->load->view('some_view', $data);
}

#6
[eluser]shailendra[/eluser]
Based on the above code given I tried doing this

In Controller
Code:
function index()
    {
       $this->load->model('category_model');
       $build_array = array();
       $proj_count = array();
       $parent_categories = $this->category_model->list_parent_categories();
       foreach($parent_categories as $parent_category)
       {
          //echo $parent_category->category_id;
          $sub_categories =  $this->category_model->list_sub_categories($parent_category->category_id);
          foreach($sub_categories as $sub_category)
          {
            $proj_count[]=$this->category_model->no_of_projects($sub_category->category_id);
          }
          $build_array[] = array (
                'parent_category' => $parent_categories,
                'sub_category' => $sub_categories,
                'count' => $proj_count
             );
        }
        $category_list = $build_array;
        //var_dump($category_list);
        $data['categories'] = $category_list;      
        $this->load->view('category_view', $data);
    }

In view

Code:
&lt;?php
foreach($categories as $category){
    echo $category['parent_category']->category_name;
        foreach ($category['sub_category'] as $lrow){
                echo $lrow->category_name;
                foreach($category['count'] as $krow){
                    echo $krow;
                }
        }
}
?&gt;


But I get following error:
----------------------------

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/category_view.php

Line Number: 3

#7
[eluser]toopay[/eluser]
If everythings goes smooth, in your view you just need to parse the "categories" variable as array (not object).

So you do...
Code:
foreach($categories as $category)
{
    echo $category['parent_category']['category_name'];
        foreach ($category['sub_category'] as $lrow)
        {
                echo $lrow['category_name'];
                foreach($category['count'] as $krow)
                {
                    echo $krow;
                }
        }
}
But i think the example i gave you, more efficient instead what you try to do. By doing what i give you above, you should only loop one multidimensional array to get the same result.

#8
[eluser]shailendra[/eluser]
I get this error now with the above view code:
------------------------------

A PHP Error was encountered

Severity: Notice

Message: Undefined index: category_name

Filename: views/category_view.php

Line Number: 4

#9
[eluser]shailendra[/eluser]
I tried your previous code you gave on 15 July. I modified the controller code to include sub category

In controller
Code:
function index()
    {
       $this->load->model('category_model');
       $data = array();
       $categories = array();
       $categories = $this->category_model->list_parent_categories();
       $i = 0;
       foreach($categories as $sub_category)
       {
          $categories['sub_category'][$i] =  $this->category_model->list_sub_categories($sub_category->category_id);
          $j=0;
          foreach($categories['sub_category'][$i] as $sub_category1)
          {
            $categories['proj_counts'][$j]=$this->category_model->no_of_projects($sub_category1->category_id);
            $j++;
          }
            $i++;
       }
       // 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 $category){
    echo $category->category_name;
    foreach($category['sub_category'] as $lrow){
            echo $lrow['category_name'];
    }
}
?&gt;

I get following 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 4

Below is var_dump($categories) output
-------------------------------------
array(11) { [0]=> object(stdClass)#17 (2) { ["category_id"]=> string(2) "10" ["category_name"]=> string(13) "Admin Support" } [1]=> object(stdClass)#18 (2) { ["category_id"]=> string(2) "15" ["category_name"]=> string(8) "Coaching" } [2]=> object(stdClass)#19 (2) { ["category_id"]=> string(2) "12" ["category_name"]=> string(27) "Engineering & Manufacturing" } [3]=> object(stdClass)#20 (2) { ["category_id"]=> string(1) "2" ["category_name"]=> string(17) "Graphic Designing" } [4]=> object(stdClass)#21 (2) { ["category_id"]=> string(2) "13" ["category_name"]=> string(5) "Legal" } [5]=> object(stdClass)#22 (2) { ["category_id"]=> string(2) "14" ["category_name"]=> string(6) "Others" } [6]=> object(stdClass)#23 (2) { ["category_id"]=> string(2) "11" ["category_name"]=> string(17) "Sales & Marketing" } [7]=> object(stdClass)#24 (2) { ["category_id"]=> string(1) "1" ["category_name"]=> string(17) "Web & Programming" } [8]=> object(stdClass)#25 (2) { ["category_id"]=> string(1) "9" ["category_name"]=> string(22) "Writing & Translations" } ["sub_category"]=> array(9) { [0]=> array(0) { } [1]=> array(0) { } [2]=> array(1) { [0]=> object(stdClass)#26 (2) { ["category_id"]=> string(2) "16" ["category_name"]=> string(14) "Product Design" } } [3]=> array(3) { [0]=> object(stdClass)#16 (2) { ["category_id"]=> string(1) "6" ["category_name"]=> string(4) "Logo" } [1]=> object(stdClass)#28 (2) { ["category_id"]=> string(1) "7" ["category_name"]=> string(7) "Website" } [2]=> object(stdClass)#29 (2) { ["category_id"]=> string(1) "8" ["category_name"]=> string(10) "Stationery" } } [4]=> array(0) { } [5]=> array(0) { } [6]=> array(0) { } [7]=> array(3) { [0]=> object(stdClass)#27 (2) { ["category_id"]=> string(1) "3" ["category_name"]=> string(3) "PHP" } [1]=> object(stdClass)#31 (2) { ["category_id"]=> string(1) "4" ["category_name"]=> string(3) "JSP" } [2]=> object(stdClass)#32 (2) { ["category_id"]=> string(1) "5" ["category_name"]=> string(3) "ASP" } } [8]=> array(0) { } } ["proj_counts"]=> array(3) { [0]=> string(1) "2" [1]=> string(1) "1" [2]=> string(1) "3" } }

#10
[eluser]toopay[/eluser]
In your view, just convert the object into an array, before looping it...
Code:
&lt;?php
$categories = (array) $categories;
foreach($categories as $category)
{
    // echo $category->category_name;
    echo $category['category_name'];
    foreach($category['sub_category'] as $lrow)
    {
            echo $lrow['category_name'];
    }
}
?&gt;


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.