jenii
jenii

Reputation: 57

How to call query function in Bonfire model?

Customers_model

 Class Customers_model extends BF_Model{

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

My query function in model

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

}

}`

Controller

public function listCustomer()
{

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

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

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

}

View

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

It can't show $list array from controller : Undefined list variable

Upvotes: 1

Views: 171

Answers (1)

molagbal
molagbal

Reputation: 331

In your Model Query method i made few changes,

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

In your Controller,

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

In Your view,try this,

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

It should work.

Upvotes: 3

Related Questions