php – How to Filter Data in CodeIgniter with 2 tables (the Category Table and Category Activity table)-ThrowExceptions

Exception or error:

I have a problem in my code , actually i have 2 tables the category table and category_activity. then the id of category is connected to the category ID in the category_activity table. now when i want to select any category then I want to show only category activity connected to this category. is it possible? This is my code

 class Category extends MY_Controller {

        protected $data;

        public function __construct()
        {
            parent::__construct();

            if (!isset($this->session->userdata['users'])){
                redirect('auth');
            }

    }

        public function index()
        {
            $this->data['category_activity'] = $this->Category_model->getcategory_activity();
            $this->data['category'] = $this->Category_model->getcategory();
            $this->load->view('templates/master', $this->data);
        }
}

This is my Category model

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

class Category_model extends CI_Model {

                 function getcategory_activity()
                 {
                       return $this->db->select('*')
                      ->from('category_activity')
                     ->get()->result();
                  }
                  function getcategory()
                  {
                           return $this->db->select('*')
                             ->from('category')
                             ->get()->result();
                   }

    }

This is my view code

</style>
<script type="text/javascript">
 $('.filter').change(function(){     
    //on each click, refresh visible / hidden for each product
    $('li.result').each(function(i, item){
      var category = $(this).data('category');
      var visible = $('input.filter[data-category="' + category + '"]:checked').length > 0;
      visible ? $(this).show() : $(this).hide();
    });
    //if no checkboxes are checked, show all products
    if($('input.filter:checked').length === 0) $('li.result').show();
});
</script>
</head>
<body>

<div class="container">    
    <div class="row">
        <div class="col-xs-12 col-md-12 col-sm-12 text-center">
            <h3 class="text-center"> Category Filter in codeigniter</h3>
        </div>
        <div class="col-md-12 col-sm-6 col-xs-12">
         <div class="col-md-3">
         <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
         <!-- Categories Panel -->
          <div class="panel panel-default">
          <!-- Heading -->
           <div class="panel-heading" role="tab" id="headingOne">
             <!-- Title -->
              <h4 class="panel-title">
                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                <span> Categories</span>
                </a>
             </h4>
            <!-- Title End -->
           </div>
           <!-- Content -->
        <div id="collapseOne" class="panel-collapse" role="tabpanel" aria-labelledby="headingOne">
           <div class="panel-body categories">
               <ul>
                <?php $cnt=0; foreach ($category as $row):$cnt+=1;?>
                    <li>
                       <input type="checkbox" class="filter" data-category="<?php echo $row->id; ?>"  > <?php echo $row->category; ?>
                    </li>
                   <?php endforeach; ?>
               </ul>
            </div>
         </div>
         </div>
        <!-- Categories Panel End -->
        <!-- Brands Panel -->
        </div>
</div>
<table class="table">
 <ul>
 <?php foreach($category_activity as $data) {?>
        <tr>
          <td><?php echo $data->category_id; ?></td>
          <td><?php echo $data->activity; ?></td>
          <td><?php echo $data->qty; ?></td>
          <td><?php echo $data->eu_qty; ?></td>
          <td><?php echo $data->unit; ?></td>
          <td><?php echo $data->description; ?></td><br>
</ul>
</td>
        </tr>
        <?php   }   ?>


        </table>
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *