1

我有两张桌子:

orders: 
orderid      customer_id amount
1       5           1
2       5           2
3       8           3
4       5           11
5       8           45
6       11          23
7       11          2

orders_items:
id      orderid      item    quantity
1       1           'item1'     1
2       1           'item2'     2
3       2           'item1'     1 
4       3           'item1'     3
5       3           'item2'     1 
6       3           'item3'     1

现在我想获取每个订单的信息,包括每个订单的所有不同商品。我用:

 $this->db->select()->from('orders')->join('orders_items', 'orders.orderid = orders_items.orderid' );
    $query = $this->db->get();
    $result_array = $query->result_array();

然而,这给了我每个订单只有最后一项的数组。我究竟做错了什么?我的最终数组应该是这样的:

Array
(
    [0] => Array
        (
            [orderid] => 49
            [customerid] => 2
            [amount] => 438.00
            [id] => 63
            array(
               array(
                 [item] => Service1
                 [quantity] => 22
                 [subtotal] => 439
                    ),
               array(
                 [item] => Service2
                 [quantity] => 22
                 [subtotal] => 439
                    )
                 )
        )

我该怎么做?

4

1 回答 1

2

需要做foreach循环,如:

foreach ($query->result_array() as $row)
{
   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
}
于 2012-06-12T09:55:54.950 回答