遇到了PHP错误

时间:2015-01-05 11:46:36

标签: php mysql codeigniter controller notice

我是codeigniter的新手并且还在学习。我想从我的数据库中显示一个包含table product的产品,其中包括product_id,product_name等表。我在下面遇到了一个问题

  

遇到PHP错误

     

严重性:注意

     

消息:未定义的变量:id

     

文件名:controllers / mysite.php

     

行号:37

以下是我的控制器:

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');


}

以下是我的模特

<?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;
        }       
    }   
}

现在我真的很困惑在哪里使用那个id,我错过了什么。请帮忙。

2 个答案:

答案 0 :(得分:0)

控制器中的

$ id未定义。

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

编辑:

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');


}

答案 1 :(得分:0)

试试这个:

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');
}
模特中的

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 '';
    }
}