-1

This is a headache issue for long time. I have the following code in codeigniter php active record.

  $this->db->select('*');
  $this->db->from('orders');
  $this->db->join('order_detail', 'order_detail.order_id = orders.o_id');
  $this->db->join('profiles','profiles.id = orders.buyer_id','left');
  $this->db->where('orders.o_id', $order_id);
  $this->db->group_by('orders.o_id');
  $query = $this->db->get();
  $order_details_by_order_id_result = (object)$query->result();

result

var_dump($order_details_by_order_id_result);exit; //see below

object(stdClass)[34]
  public 0 => 
    object(stdClass)[37]
      public 'xx_id' => string '13' (length=2)
      public 'yy_iud' => string '22' (length=10)
      public 'order_total' => string '25.00' (length=5)
      public 'shipto_fname' => string 'dan' (length=3)
      public 'shipto_lname' => string 'theman' (length=6

on my controller i called the above function as follow:

  $order_details = $this->orderdetails->get_orderdetail_of_buyer($oid);
  $data['order_details'] = $order_details; //pass this to the view
  $this->load->view('dashboard/order_detail_view',$data);

and i want to send the result to the view (/order_detail_view.php) Order #o_id ;?>

$order_details->o_id; //why on earth this expression show me error

i guess the problem is

object(stdClass)[34] public 0 =>

How could i ever solve this because i fetch only one order at a time. Thanks

Develop4Life
  • 7,581
  • 8
  • 58
  • 76
  • 1
    I think we will need more code. I don't see a reference to an object named `$order_details` anywhere so we can't know why it doesn't contain a value. – William_Wilson Sep 25 '15 at 12:27

2 Answers2

1

In CodeIgniter, $query->result() returns an array of objects, each element represents the row in the result set. No need to convert it into object; use...

$result = $query->result();
if ($result) {
  $order_details_by_order_id_result = $result[0];
}

... instead.


For your original code, there's actually no way to get the content object: PHP objects that are results of conversion from array do not play nicely with numeric keys (you can read more about this here).

Community
  • 1
  • 1
raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • no that is a manual way of doing that i want to avoid that do;t u think ? – Develop4Life Sep 25 '15 at 12:31
  • Avoid what, always getting the first result? Do you want to show those in come rows instead? – raina77ow Sep 25 '15 at 12:34
  • yeah but you runied some of my logic here ,forxple. you fogote that my result to the view is all object , you assigned it the first element which is array as result[0] – Develop4Life Sep 25 '15 at 12:37
  • man i dont like working with array i like working with objects , would not that be enough ? – Develop4Life Sep 25 '15 at 12:44
  • With all due respect, that's like saying "I don't like working with division, so that's always use multiplication". Technically, it's possible, but what's the point? Shouldn't you always use the best tool available? – raina77ow Sep 25 '15 at 12:45
  • deinfly yes all my code use object instead of array , would not i need to adopt the same style across pages ? for just this one simple reason. – Develop4Life Sep 25 '15 at 12:49
  • It's not about style. It's about simplicity. You can wrap all the data you deal with in some objects (stdClass or your own), but that'll only compicate the code. What you should do is choose the types that fit the data structure. In this particular case, the data is a sequence of rows; while each row can be represented as an object, it's not a good choice for a value representing the _collection_; array fits better here. – raina77ow Sep 25 '15 at 12:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90629/discussion-between-raina77ow-and-danielad). – raina77ow Sep 25 '15 at 14:25
1

row() method will return a single row for you query.

$query = $this->db->get();
if ($query->num_rows() > 0)
{
   $row = $query->row(); 
   echo $row->xx_id;
}
Alex Chorry
  • 334
  • 4
  • 9