没有从视图codeigniter获取数组值

时间:2013-04-03 11:58:55

标签: codeigniter

我的模型功能是

    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 

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

3 个答案:

答案 0 :(得分:1)

尝试在控制器中加载模型,如

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

在不加载模型的情况下,您无法从get_user函数

获取详细信息

答案 1 :(得分: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; ?>

答案 2 :(得分:-1)

尝试在主/客户中使用$data代替$details