如何在Bonfire模型中调用查询功能?

时间:2017-06-21 04:06:50

标签: php codeigniter bonfire

Customers_model

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

它无法从控制器显示<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X46/junos"> <multi-routing-engine-results> <multi-routing-engine-item> <re-name>node0</re-name> <environment-information xmlns="http://xml.juniper.net/junos/12.1X46/junos-chassis"> <environment-item> <name>Routing Engine</name> <class>Temp</class> <status>OK</status> <temperature junos:celsius="58">58 degrees C / 136 degrees F</temperature> </environment-item> <environment-item> <name>Routing Engine CPU</name> <status>Absent</status> </environment-item> <environment-item> <name>Power Supply 0</name> <class>Power</class> <status>OK</status> </environment-item> </environment-information> </multi-routing-engine-item> <multi-routing-engine-item> <re-name>node1</re-name> <environment-information xmlns="http://xml.juniper.net/junos/12.1X46/junos-chassis"> <environment-item> <name>Routing Engine</name> <class>Temp</class> <status>OK</status> <temperature junos:celsius="57">57 degrees C / 134 degrees F</temperature> </environment-item> <environment-item> <name>Routing Engine CPU</name> <status>Absent</status> </environment-item> <environment-item> <name>Power Supply 0</name> <class>Power</class> <status>OK</status> </environment-item> </environment-information> </multi-routing-engine-item> </multi-routing-engine-results> <cli> <banner>{primary:node0}[edit]</banner> </cli> 数组:未定义的列表变量

1 个答案:

答案 0 :(得分: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 />';
}

它应该有用。