1

This is my categories. If I click on a category then it goes to its view page, and show all products related with this selected category. enter image description here

I am trying like this, but it is not working. I am using 3 tables for this situation.

1.categoriesenter image description herecategories table

2.products enter image description here 3.product_cat

enter image description here

Controller:

<?php 
class Clothing extends Controller{
    
    function product_details(){
        $id=$this->uri->segment(3);
        
        $this->load->model('Products_model');
        $data['products']=$this->Products_model->product_details($id);
        
        //$this->load->view('clothe',$data);
    }
}

?>

Model:

<?php
class Products_model extends Model {
    function product_details($id){
                    $query=$this->db->select("*")
                    ->from("product_cat")
                    ->where("categories_id",$id);
                    return $query;
        }
    }

View:

<html>
    <head></head>
    <body>
        
        <?php  foreach ($products as $v_menu) { ?>
                    <?php echo $v_menu; ?>
        <?php } ?>
    </body>
</html>
5
  • In model modify your query as $query=$this->db->select("*") ->from("product_cat") ->where("categories_id",$id)->get()->result(); Commented Sep 20, 2015 at 16:32
  • 1
    Oh brother. thanks lot. may god bless you. Thanks again. Commented Sep 20, 2015 at 16:40
  • Version of CodeIgniter you are using is old at least more than 6 years now and it is not considered as secure one. Port your code to v 3.0.x Commented Sep 20, 2015 at 17:42
  • can you please, i got the product id's related with category id? and now how can i fetch the product details using this product id from the table products given above in the image? Commented Sep 20, 2015 at 18:22
  • I tried like this: $results= $this->db->select("*") ->from("products") ->where("id",$key)->get()->result(); Commented Sep 20, 2015 at 18:58

1 Answer 1

1

You can join the two tables products and product_cat as below so that you can get the product details too-

return $query=$this->db->select('*')
        ->from('product a')
        ->from('product_cat p')
        ->where("p.product_id = a.id")
        ->where('p.categories_id =', $id)
        ->get()->result();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.