1

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

jenii
  • 57
  • 1
  • 11
  • What errors are you in your error log? – Matt Jun 21 '17 at 04:11
  • @mkaatman: Undefined variable: list – jenii Jun 21 '17 at 04:12
  • Oh, shouldn't that foreach by something like `foreach($data['list'] as $value)` it's incomplete, so I don't know what other code you've got that may be setting list but php seems to think no variable named list exists. – Matt Jun 21 '17 at 04:14
  • @mkaatman same error Undefined variable: data – jenii Jun 21 '17 at 04:17
  • After looking at the docs, it seems like you were doing it correctly. https://www.codeigniter.com/user_guide/tutorial/news_section.html If you `var_dump($data['list']);` immediately after setting it, does it contain anything? – Matt Jun 21 '17 at 04:21
  • @mkaatman still same error , have no idea why you use var_dump – jenii Jun 21 '17 at 04:27
  • var_dump will show you the contents of that variable. If that variable is empty after you set it and then you try to reference list, it's going to puke. – Matt Jun 21 '17 at 04:28

1 Answers1

3

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.

molagbal
  • 323
  • 3
  • 11
  • can you just write this in your controller? echo "
    ";print_r($list);die; after the second line...and show me what it is giving...
    – molagbal Jun 21 '17 at 13:43
  • yeah it showed array of customer :Array ( [0] => stdClass Object ( [first_name] => david [last_name] => brime [customer_id] => 277 ) [1] => stdClass Object ( [first_name] => tom [last_name] => term [customer_id] => 278 ) [2] => stdClass Object ( [first_name] => Megan [last_name] => lee [customer_id] => 280 ) ) – jenii Jun 21 '17 at 13:55
  • very good,can you do the same in your view page,so we will sure this array of object is going to your view..put it in your view,= "
    ";print_r($list);die;?>
    – molagbal Jun 21 '17 at 16:51
  • yeah it still Undefined variable: list – jenii Jun 21 '17 at 16:57
  • please check if there is any problem regarding view name..code looks fine :)....can you attach a screenshot of your view? – molagbal Jun 21 '17 at 17:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147296/discussion-between-molagbal-and-jenny). – molagbal Jun 21 '17 at 17:22