-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
  • 7,034
  • 12
  • 38
  • 58
Babar Ali
  • 181
  • 1
  • 18
  • 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
  • 3,037
  • 7
  • 41
  • 69
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,263
  • 8
  • 57
  • 98
  • pass some value to $id – Pathik Vejani Jan 05 '15 at 11:53
  • yes when i pass pass some $id ='1'; or any other product_id value i get the desired product. But this is not dynamic, – Babar Ali Jan 05 '15 at 12:02
  • that is why i am telling you that pass id value dynamically. – Pathik Vejani Jan 05 '15 at 12:09
  • can you post HTML code also? – Pathik Vejani Jan 05 '15 at 12:10
  • when you submit the form that time also pass the value for the product id – Pathik Vejani Jan 05 '15 at 12:11
  • below is the html code

    product_name; ?>

    – Babar Ali Jan 05 '15 at 12:11
  • edit question and write proper HTML code. – Pathik Vejani Jan 05 '15 at 12:14
  • where is product id? – Pathik Vejani Jan 05 '15 at 12:14
  • There is no submit form .. Im trying to fetch product details from the database table named product_id .. this is the image of the site i have hovered mouse to show the link http://i60.tinypic.com/zsobc2.png and below is the page where i am navigated , where the product is shown and http://i60.tinypic.com/xoocw8.png – Babar Ali Jan 05 '15 at 12:20
  • do you want all products information? – Pathik Vejani Jan 05 '15 at 12:38
  • $id = $this->uri->segment(6); try this for id value. – Pathik Vejani Jan 05 '15 at 12:42
  • tried it .. as i dont see the above error but i also dont see any product info fetched.. but i do get the second error from the 2nd pic posted above : 'A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach() Filename: mysite/product-content.php' – Babar Ali Jan 05 '15 at 12:48
  • see whether my query works or not. print_r the variable and exit. you will get the data all related to product. – Pathik Vejani Jan 05 '15 at 12:58
  • Finally Found MY Mistake .. i removed $id = $_REQUEST['product']; and all similar $id's and only passed $id in function->model then function product($id) <--[controller] and lastly $data['rows3'] = $this->products->product($id); ... The error was in mysql query there was addition of concatinated "" . so the query now looks like below : $q3 = $this->db->query('SELECT * FROM product WHERE product_id ='.$id.''); – Babar Ali Jan 05 '15 at 12:58
  • Thankyou all of you who helped me :) couldnt have done without your effort guyz .. love this site !!! This was my first question and got satisfied :) Special thanks to @nody , Kiren Shiva – Babar Ali Jan 05 '15 at 13:00