-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

1
  • 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. Sep 25, 2015 at 12:27

2 Answers 2

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;
}
1
  • you previous answer should fit better than this Sep 25, 2015 at 12:36
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).

8
  • no that is a manual way of doing that i want to avoid that do;t u think ? Sep 25, 2015 at 12:31
  • Avoid what, always getting the first result? Do you want to show those in come rows instead?
    – raina77ow
    Sep 25, 2015 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] Sep 25, 2015 at 12:37
  • man i dont like working with array i like working with objects , would not that be enough ? Sep 25, 2015 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, 2015 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.