user2092317
user2092317

Reputation: 3338

how to print an array of objects in codeigniter- php?

I am facing a problem to fetch array in codeigniter

$data['one']=$this->db->query($sql1);
$data['tho']=$this->db->query($sql2);
$data['three']=$this->db->query($sql3);

I am trying to do this following code I am getting error

Fatal error: Call to a member function result() on a non-object in

If I do var_dump($data['one']); I am getting the following displayed:

Array
(
 [0] => stdClass Object
 (
  [date] => 2013-09-28
 )
 [1] => stdClass Object
 (
  [date] => 1970-01-01
 )
 [2] => stdClass Object
 (
  [date] => 2013-09-28
 ) 
)

Upvotes: 0

Views: 13473

Answers (1)

Vassilis Barzokas
Vassilis Barzokas

Reputation: 3242

You can do (in the controller) something like this foreach one of your arrays:

  foreach ($data['one']->result() as $row)
  {
        echo $row->date; //or whatever the query returns
  }

If you want to print it on the view you do it like this:

  foreach ($one->result() as $row)
  {
        echo $row->date; //or whatever the query returns
  }

See more in the official documentation about queries here

Upvotes: 2

Related Questions