title: Codeigniter filter by category id for product table
body: <p>I have this code which pulls out all the values from a table in my database and it works fine however, I don't want all the the products showing. I want to filter by <code>category_id</code>. The name of the table is called <code>products</code>. Here is the view file that shows all the products: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php foreach($products as $product) : ?&gt;
   &lt;div class="col-md-4 game"&gt;
    &lt;div class="game-price"&gt;&lt;?php echo $product-&gt;price; ?&gt;&lt;/div&gt;
     &lt;a href="&lt;?php echo base_url(); ?&gt;products/details/&lt;?php echo $product-&gt;id; ?&gt;"&gt;
      &lt;img src="&lt;?php echo base_url(); ?&gt;assets/images/products/&lt;?php echo $product-&gt;image; ?&gt;" /&gt;
     &lt;/a&gt;
     &lt;div class="game-title"&gt;
      &lt;?php echo $product-&gt;title; ?&gt;
     &lt;/div&gt;
     &lt;div class="game-add"&gt;
      &lt;form method="post" action="&lt;?php echo base_url(); ?&gt;cart/add"&gt;
         QTY: &lt;input class="qty" type="text" name="qty" value="1" /&gt;&lt;br&gt;
         &lt;input type="hidden" name="item_number" value="&lt;?php echo $product-&gt;id; ?&gt;" /&gt;
         &lt;input type="hidden" name="price" value="&lt;?php echo $product-&gt;price; ?&gt;" /&gt;
         &lt;input type="hidden" name="title" value="&lt;?php echo $product-&gt;title; ?&gt;" /&gt;
         &lt;button class="btn btn-primary" type="submit"&gt;Add To Cart&lt;/button&gt;
        &lt;/form&gt;
     &lt;/div&gt;
   &lt;/div&gt;
  &lt;?php endforeach; ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>And here is a screenshot of my table:
  <a href="https://drive.google.com/open?id=0BwBGhbPaJU_jRUZxemk3TGVqams" rel="nofollow noreferrer">Database Screenshot</a></p>
  
  <p>How can I edit the code above so that it displays results for a specific category?</p>
  
  <p>Here is my controller: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  class Products extends CI_Controller{
   public function index(){
    //Get All Products
    $data['products'] = $this-&gt;Product_model-&gt;get_products();
  
    //Load View
    $data['main_content'] = 'products';
    $this-&gt;load-&gt;view('layouts/main', $data);
   }
  
   public function details($id){
    //Get Product Details
    $data['product'] = $this-&gt;Product_model-&gt;get_product_details($id);
  
    //Load View
    $data['main_content'] = 'details';
    $this-&gt;load-&gt;view('layouts/main', $data);
   }
  
   public function __construct()
      {
          parent::__construct();
  
          $this-&gt;load-&gt;helper('form');
  
          $this-&gt;load-&gt;model('Search_model');
      }
  
      public function execute_search()
      {
          // Retrieve the posted search term.
          $search_term = $this-&gt;input-&gt;post('search');
  
          // Use a model to retrieve the results.
          $data['results'] = $this-&gt;Search_model-&gt;get_results($search_term);
  
          // Pass the results to the view.
          $this-&gt;load-&gt;view('products',$data);
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here is my model:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  class Product_model extends CI_Model{
   /*
    * Get All Products
    */
    public function get_products(){
    $this-&gt;db-&gt;select('*');
    $this-&gt;db-&gt;from('products');
    $query = $this-&gt;db-&gt;get();
    return $query-&gt;result();
    }
    
    /*
     * Get Single Product
     */
     public function get_product_details($id){
    $this-&gt;db-&gt;select('*');
    $this-&gt;db-&gt;from('products');
    $this-&gt;db-&gt;where('id', $id);
    
    $query = $this-&gt;db-&gt;get();
    return $query-&gt;row();
     }
     
     /*
      * Get Categories
      */
      public function get_categories(){
    $this-&gt;db-&gt;select('*');
    $this-&gt;db-&gt;from('categories');
    $query = $this-&gt;db-&gt;get();
    return $query-&gt;result();
      }
      
      /*
    * Get Most Popular Products
   */
   public function get_popular(){
    $this-&gt;db-&gt;select('P.*, COUNT(O.product_id) as total');
    $this-&gt;db-&gt;from('orders AS O');
    $this-&gt;db-&gt;join('products AS P', 'O.product_id = P.id', 'INNER');
    $this-&gt;db-&gt;group_by('O.product_id');
    $this-&gt;db-&gt;order_by('total', 'desc');
    $query = $this-&gt;db-&gt;get();
    return $query-&gt;result();
   }
   
   /*
    * Add Order To Database
    */
    public function add_order($order_data){
    $insert = $this-&gt;db-&gt;insert('orders', $order_data);
          return $insert;
   }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>This is the part that actually gets the products in my model and the part I think I have to edit:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public function get_products(){
    $this-&gt;db-&gt;select('*');
    $this-&gt;db-&gt;from('products');
    $query = $this-&gt;db-&gt;get();
    return $query-&gt;result();</code></pre>
  </div>
  </div>
  </p>