1

我的模型函数是

    public function getUser()
{
    $this->db->select(array('customer_name','customer_address','phone_no','fax','email_id','contact_person_name','mobile_no'));     
    $this->db->from('add_customer');
    $this->db->where(array('status'=>1));
    $res = $this->db->get();
    if($res->num_rows() > 0)
    { 
        $rows = $res->result_array();
        return $rows;
    }else{
        return false;
    }
}

控制器中的功能

public function search_customer()
{

    //$this->load->helper('url');
    $data['baseurl'] = base_url();
    $this->load->view('templates/header');
    $data['details'] = $this->Customer_model->getUser();
    $this->load->view('master/customer',$data);
} 

从我的视图中提取的数据是这样的

     <?php for($i=0;$i<count($details);$i++){ ?>
  <tr>
    <td><?php echo ($i+1); ?></td>
    <td><?php echo $details[$i]['customer_name']; ?></td>
    <td><?php echo $details[$i]['customer_address']; ?></td>
    <td><?php echo $details[$i]['phone_no']; ?></td>
    <td><?php echo $details[$i]['email_id']; ?></td>
    <td><?php echo $details[$i]['contact_person_name']; ?></td>
    <?php }?>

这会导致这样的错误

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: details

Filename: master/Customer.php

Line Number: 69 

有人请告诉我为什么会出现这个错误

4

3 回答 3

1

尝试将模型加载到控制器中,例如

$this->load->model('Customer_model');

如果不加载模型,您将无法从 get_user 函数获取详细信息

于 2013-04-03T12:34:07.277 回答
0

模型:

public function getUser()
{
    $this->db->select('customer_name','customer_address','phone_no',
            'fax','email_id','contact_person_name','mobile_no');     
    $this->db->from('add_customer');
    $this->db->where('status',1);
    return $this->db->get()->result();
}

控制器:

public function search_customer()
{
    $data['baseurl'] = base_url();
    //Load the view in variable as data, but do not render 
    //($header is accessible in 'master/customer' view)
    $data['header'] = $this->load->view('templates/header','',true);
    //Be sure to load Customer_model
    $data['details'] = $this->Customer_model->getUser();
    $this->load->view('master/customer',$data);
} 

看法:

<?php if(count($details)): ?>
   <?php foreach($details as $detail): ?>
   <tr>
       <td><?php echo $detail->customer_name; ?></td>
       <td><?php echo $detail->customer_address; ?></td>
       <td><?php echo $detail->phone_no; ?></td>
       <td><?php echo $detail->email_id; ?></td>
       <td><?php echo $detail->contact_person_name; ?></td>
   </tr>
   <?php endforeach; ?>
<?php endif; ?>
于 2013-04-04T12:21:43.283 回答
-1

尝试在您的主/客户中使用$data而不是。$details

于 2013-04-03T12:04:15.150 回答