1

客户模型

 Class Customers_model extends BF_Model{

 protected $table_name  = 'customers';
protected $key          = 'customer_id';
protected $date_format  = 'datetime';

我在模型中的查询功能

function get_customerlist()
{
    $sql = $this->db->query('SELECT first_name, last_name , customer_id FROM customers ');
    return $sql->result();

}

}`

控制器

public function listCustomer()
{

    $this->load->model('customers_model'); // whatever you call it

  $data['list'] =  $this->customers_model->get_customerlist();

    $this->load->view('myview', $data);

}

看法

 foreach($list as $value)
    {
        echo  $value->first_name. '<br />'; output.
    }

它无法显示$list来自控制器的数组:未定义的列表变量

4

1 回答 1

3

在您的Model查询方法中,我做了一些更改,

function get_customerlist()
    {
        $selelct = array('first_name','last_name','customer_id');
        return $this->db->select($select)
                       ->from('customers')
                       ->get()
                       ->result();   
    }

在您的控制器中,

public function listCustomer()
 {
  $this->load->model('customers_model'); // whatever you call it
  $list = $this->customers_model->get_customerlist();
  $data['list'] =  $list; //returned result is an array of object
  $this->load->view('myview', $data);
}

在你看来,试试这个,

foreach((array)$list as $item)
{
     echo  $item->first_name. '<br />';
}

它应该工作。

于 2017-06-21T05:11:07.447 回答