I have two tables one is products another is categories. I save the category id to the post tables category field. I have joined both the table in Post_model and able to fetch the category name in listing page when I run foreach() but it is giving an error when I want to fetch it in details page(slug page). It says the undefined variable name in product details page.
Product_model
class Product_model extends CI_Model{
public function __Construct(){
$this->load->database();
}
public function get_products($slug = FALSE){
if($slug === FALSE){
$this->db->order_by('products.id', 'DESC');
$this->db->join('categories','categories.id = products.category');
$query= $this->db->get('products');
return $query->result_array();
}
$query= $this->db->get_where('products', array('slug' => $slug));
return $query->row_array();
}
In Controller
public function index(){
$data['subview']= 'pages/listing';
$data['products'] = $this->Product_model->get_products();
$this->load->view('layout_main',$data);
print_r($data['products']);
}
public function view($slug= NULL){
$data['post'] = $this->Product_model->get_products($slug);
if(empty($data['post'])){
show_404();
}
$data['title'] = $data['post'] ['title'];
$this->load->view('pages/product_view',$data);
}
in View
<?php foreach ($cv_products as $post) : ?>
<div class="col-4">
<a class="all-link--pro" href="<?php echo site_url("product_view/".$post['slug'])?>"> //pro slug link
<img class="img-fluid img-size" src="<?php echo base_url("assets/img/".$post['main_img'])?>">//pro img
<p><?php echo $post['title'] ?></p>//pro title
<p><?php echo $post['name'] ?></p>//pro cat
<p>Rs. <?php echo $post['price']; ?></p>//pro price
</a>
</div>
<?php endforeach; ?>
It's working perfectly I get all product list and the respective category but after clicking the slug link or <a class="all-link--pro" href="<?php echo site_url("product_view/".$post['slug'])?>">
i get all except error on name field
<p><?php echo $post['title'] ?></p>
<p><?php echo $post['price'] ?></p>
<p><?php echo $post['name'] ?></p>
Just wanna get the categories.name from categories table where products.category is quals to categories id
forasmuch as you are using foreach you have to change $query to
$query= $this->db->get_where('products', array('slug' => $slug));
return $query->result_array();
if you want to get the subject name:
problem is in the model file.
if slug !== false no join will happen.
change get_products() after your if block to:
$this->db->join('categories','categories.id = products.category');
$this->db->where(array('slug' => $slug));
$query = $this->db->get("products")->row_array();
return $query;
UPDATE:
you get that error because you are trying to use foreach for an associative array.
in slug page, your result is like this:
['yourTitle',1000,'yourName']
when you use foreach, the output will be a string each time in the loop.
it's better to have a separate view for your slug page, but if you are not going to do that, change $query
to:
$query = $this->db->get("products")->result_array();