-1

I am new to codeigniter and still learning. I want to show a single product from my database containing the table product which includes product_id,product_name etc tables.I have come up with a problem below

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: id

Filename: controllers/mysite.php

Line Number: 37

Below is my controller :

public function product()

{   
    $this->load->model('products');
    $data['rows3'] = $this->products->product($id);
    $this->load->view('mysite/include/product-left');
    $this->load->view('mysite/product-content',$data);
    $this->load->view('mysite/include/product-related');
    $this->load->view('mysite/include/footer');


}

Below is my model

<?php
 class products extends CI_Model{

   function product($id){

    $q3 = $this->db->query("SELECT * FROM product WHERE product_id  ='".$id."'");

    if($q3->num_rows() > 0){

        foreach($q3->result() as $row){

           $data[] = $row;

        }
        return $data;
        }       
    }   
}

Now i am really confused where to use that id, what am i missing. please help.

Shaiful Islam
  • 6,826
  • 12
  • 36
  • 56
Babar Ali
  • 181
  • 1
  • 17
  • Where are you defining $id? – ggdx Jan 05 '15 at 11:47
  • are you passing any value or id to the controller action via url? – Sougata Bose Jan 05 '15 at 11:48
  • That is where i am getting confused.. i want to match the $id with the product_id from the database table. And yes i am passing the product_id value in the url . so any product link clicked goes to that product_id in the end of the url – Babar Ali Jan 05 '15 at 11:52

2 Answers2

0

in your controller $id is not defined.

$data['rows3'] = $this->products->product($id);

EDIT:

public function product()

{   

    $id = $_REQUEST['product']; // You have to receive id something like this. Because it is a controller action. Isn't it?

    $this->load->model('products');
    $data['rows3'] = $this->products->product($id);
    $this->load->view('mysite/include/product-left');
    $this->load->view('mysite/product-content',$data);
    $this->load->view('mysite/include/product-related');
    $this->load->view('mysite/include/footer');


}
Kiren S
  • 2,939
  • 5
  • 40
  • 68
0

try this:

public function product()    
{   
    $this->load->model('products');
    $id = $this->uri->segment(6);
    $data['rows3'] = $this->products->product($id);
    $this->load->view('mysite/include/product-left');
    $this->load->view('mysite/product-content',$data);
    $this->load->view('mysite/include/product-related');
    $this->load->view('mysite/include/footer');
}

in model:

function product($iUserID)
{
    $q = $this->db->query("SELECT * FROM product WHERE product_id  ='".$id."'");
    if($q->num_rows > 0)
    {
        return $q->row_array();
    }
    else
    {
        return '';
    }
}
Pathik Vejani
  • 4,194
  • 8
  • 52
  • 89