1

I need to get the the subcategories of its categories in codeigniter. I am stuck at passing category id to the my model method

my code is below :

this is my view : index.php

<ul class="list-inline">

                    <?php 
                        if($category->num_rows() > 0)
                        {
                            foreach ($category->result() as $row) 
                            {
                                ?>
                                    <li><a href="<?php echo base_url();?>main/getsubcategory<?php $row->id; ?>"><img class="img-responsive center-block" width="80px" src="<?php echo base_url(); ?>assets/admin/uploads/category/<?php echo $row->cat_home_image; ?>" alt="icon" title="icon" class="img-responsive" /><p style="font-size:16px;"><?php echo $row->category_description; ?> </p></a></li>

                                <?php 
                            }
                        }
                    ?>

                </ul>

this is my controller : Main.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends CI_Controller {

    public function index()
    {
        $this->load->model("category_model");
        $data["category"] = $this->category_model->fetch_category();
        $this->load->model("ads_model");
        $data["ads"] = $this->ads_model->fetch_home_ads();
        $this->load->view('index',$data);
        //$this->load->view('category');
    }

    public function getsubcategory()
    {       
        $this->load->model("category_model");
        $data["subcategory"] = $this->category_model->fetch_subcategory();
        $this->load->view('category',$data);
    }
}

this is my Model : category_model.php

    <?php 

class Category_model extends CI_Model
{

    function fetch_category()
    {
        $query = $this->db->query("SELECT * FROM category AS c INNER JOIN category_language AS cl ON c.id=cl.category_id where parent_id IS NULL && lang_id='1' && c.id!='4'  ORDER BY category_description");
        return $query;
    }

    function fetch_subcategory()
    {
        $subcat = $this->db->query("SELECT * FROM category AS c INNER JOIN category_language AS cl ON c.id=cl.category_id where parent_id='1' && lang_id='1'  ORDER BY category_description");
        return $subcat;
    }
}

?>

How to pass the category id to model to get the appropriate subcatory of category_id

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Linu S
  • 45
  • 1
  • 14

1 Answers1

1

Hope this will help you :

Your view should be like this :

<ul class="list-inline">
  <?php 
      if($category->num_rows() > 0)
      {
          foreach ($category->result() as $row) 
          {?>
            <li>
               <a href="<?=site_url('main/getsubcategory/'.$row->id);?>">
               <img class="img-responsive center-block" width="80px" src="<?=site_url('assets/admin/uploads/category/'.$row->cat_home_image); ?>" alt="icon" title="icon" class="img-responsive" />
               <p style="font-size:16px;"><?php echo $row->category_description; ?> </p>
               </a>
            </li>

          <?php 
          }
      }
  ?>
</ul>

Your controller's getsubcategory method should be like this :

public function getsubcategory($id)
{    
    if ($id) 
    {   
      $this->load->model("category_model");
      $data["subcategory"] = $this->category_model->fetch_subcategory($id);
      $this->load->view('category',$data);
    }
}

Your model method fetch_subcategory should be like this :

Note : make sure all column name belongs to correct table alias

function fetch_subcategory($id)
{
    $this->db->select('*');
    $this->db->from('category c'); 
    $this->db->join('category_language cl', 'cl.category_id = c.id');

    /* Use $id here like this
       $this->db->where('parent_id', $id);
    */

    $this->db->where('c.parent_id', '1');
    $this->db->where('cl.lang_id', '1'); 
    $this->db->order_by('c.category_description', 'DESC');
    $result = $this->db->get()->result();
    return $result;
}
Pradeep
  • 9,667
  • 13
  • 27
  • 34