使用codeigniter中的活动记录从mysql中获得的复杂数据集


Complex data set from mysql using active record in codeigniter

我正在生成一个发票生成应用程序。在这个表中,我有一个表,它存储了带有buyer和invoice_no标志的发票的各个项目。这些标志帮助我从行中获取汇总发票构造。这就是功能:

public function invoice_get()
{
    $data=array();

    $this->db->group_by('invoice_no');
    $this->db->order_by('invoice_no','asc');
    $Q=$this->db->get('sales');
    echo $Q->num_rows();
    if($Q->num_rows()>0)
    {
        foreach($Q->result_array() as $row)
        {
            if(!isset($data[$row['invoice_no']]))
            {
                $data[$row['invoice_no']]['date']=$row['date'];
                $this->db->where('id',$row['buyer']);
                $q=$this->db->get('ledgers');
                $data[$row['invoice_no']]['buyer']=$q->row();
            }
            else
            {
                $this->db->where('id',$row['item_id']);
                $i=$this->db->get('inventory');
                $data[$row['invoice_no']]['items'][$row['item_id']]=$i->row();
            }
        }
        print_r($data);
        //$this->response($data);
    }

}

问题是我没有得到物品的详细信息可能是什么错误?

$i = 1; 
foreach ($q->result_array() as $row){
$data[$row['invoice_number']]['date'] = $row['date'];
$data[$row['invoice_number']]['buyer']['id'] = $row['id'];
$data[$row['invoice_number']]['item'][$i]['name'] = $row['name'];
$data[$row['invoice_number']]['item'][$i]['description'] = $row['description']; 
//and more if you want to add....
$i++;
}

这应该会给你想要的结果。