0

I'm trying to pull customer orders from my database in an array. My database structure has an orders table, then an order_items table that joins on the order ID fields of both tables.

public function getUserOrders($id) {
    $this->db->select('*');
    $this->db->from('orders');
    $this->db->join('order_items', 'order_items.order_id = orders.id', 'left');
    $this->db->join('products', 'products.id = order_items.product_id', 'left');
    $this->db->where('cust_id', $id);
    $this->db->limit(5);
    $query = $this->db->get();
    $result = $query->result_array();
    return $result;
  }

I want my array structure to be nested, so that in each order array there is a nested array of products for that order, can I achieve this in my model above?

1
  • While the ORM answer is valid that solution might be overkill if you don't need to restructure datasets on a regular basis. It is not that hard to restructure result using simple loops. If you could share an example result set from the above a query an accurate answer may be possible. Commented Feb 19, 2018 at 23:02

1 Answer 1

1

CodeIgniter can’t do nested arrays easily out of the box from a DB request. I would consider using an ORM. A quick search suggests a few, but rather helpfully this Stack Overflow answer might be the best to flick through.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.