2012-07-26 69 views
0

拉取數據,數據是這樣的:需要幫助的PHP數組從MySQL數據從MySQL表概括

Acct #   Acct Name     Item  Date Sold   Qty Sold 
10293   Company A    Canon JX200 2012-06-24    1 
11042   Company B    Canon JX200 2012-06-05    2 
12560   Company D    Canon L90 2012-04-20    1 
12560   Company D    Canon L90 2012-05-07    1 
14160   Company D    Canon L90 2012-05-22    2 
14168   Company C    Canon L90 2012-04-20    6 

我想輸出,看起來像:

Acct # Acct Name Item   Mo 1 Mo 2 Mo 3 Mo 4 Mo 5 Mo 6 Tot 
10293 Company A Canon JX200           1  1 
11042 Company B Canon JX200           2  2 
12560 Company C Canon L90        6      6 
12560 Company D Canon L90        1  3    4 

杆進洞2線性代數課在大學和PHP陣列讓我難住。需要將我的數據放入數組中,進行排序和總結。最大的問題(我認爲)是構建數組的代碼。提前致謝!

回答

0

嘗試通過addinf對MySQL查詢中的數據進行排序ORDER BY 科目名稱。當您收到數據時,此代碼應該有所幫助:

$data = array(); 
foreach ($rows as $row) { 
preg_match('/^\d{4}\-(\d{2})\-/', $row['DateSold'], $match); 
    $data[] = array(
    'Acct' => $row['Acct'], 
    'AcctName' => $row['AcctName'], 
    'Item' => $row['Item'], 
    'Mo ' . $match[1] => $row['QtySold'] 
); 

}