2012-04-25 62 views
5

這裏的任何人都可以幫我解決這個問題嗎?我只想得到某一欄的總數或總和,請參考下圖。一直試圖排序這2天,但不能 得不到運氣,希望任何人都可以幫助我這一點。我非常感謝,並在 提前感謝。數組列總和

這裏是一個示例圖像http://www.freeimagehosting.net/pig81

<?php $sql = "SELECT name, doc_date, descs, debit, credit FROM statement WHERE 
member_id = $member_id ORDER BY doc_date"; 

$query = mysql_query($sql); 
$combinedResults = array(); 

while($result = mysql_fetch_array($query)) { 
$combinedResults[$result[`name`]][] = array(`name` => $result[`name`], `doc_date` => 
$result[`doc_date`], `descs` => $result[`descs`],`debit` => $result[`debit`], `credit` 
=> $result[`credit`]);} 

foreach(array_keys($combinedResults) as $groupKey) { ?> 
<table> 
    <tr><?php foreach($combinedResults[$groupKey] as $item) {?> 
    <td>Date</td> 
    <td>Description</td> 
    <td>Debit</td> 
    <td>Credit</td> 
    <td>Balance</td> 
    </tr> 
<tr> 
<td colspan="2"><?php echo $groupKey; ?></td> 
<td width="105">&nbsp;</td> 
<td width="105">&nbsp;</td> 
<td width="105">&nbsp;</td> 
</tr> 
<tr><?php foreach($combinedResults[$groupKey] as $item) {?> 
<td><?php echo $item[`doc_date`];?></td> 
<td><?php echo $item[`descs`];?></td> 
<td><?php echo $item[`debit`];?></td> 
<td><?php echo $item[`credit`]; ?></td> 
<td>&nbsp;</td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>sum of debit goes here</td> 
</tr> 
<?php }} ?> 
</table> 

回答

1

我根據我在其中看到的內容重構了代碼,並添加了一個餘額計算器,但實際上我沒有對它進行測試。

<?php 

$sql = "SELECT name, doc_date, descs, debit, credit 
     FROM statement 
     WHERE member_id = $member_id 
     ORDER BY doc_date"; 

$query = mysql_query($sql); 
$combinedResults = array(); 

// Slurp SQL results into array 
while ($result = mysql_fetch_array($query)) { 
    $combinedResults[$result['name']][] = array(
    'name' => $result['name'], 
    'doc_date' => $result['doc_date'], 
    'descs' => $result['descs'],'debit' => $result['debit'], 
    'credit' => $result['credit'] 
); 
} 

// Define a format for all table lines (add CSS as required) 
$fmt = "<tr>\n <td>%s</td>\n <td>%s</td>\n <td>%s</td>\n <td>%s</td>\n <td>%s</td>\n</tr>"; 

print "<style type='text/css'>TD{width:105px;}</style>\n"; 

print "<table>\n"; 

// Walk through array... 
foreach ($combinedResults[$groupKey] as $item) { 
    // Start a section... 
    printf($fmt, "Date", "Description", "Debit", "Credit", "Balance"); 
    printf($fmt, $groupKey, "", "", "", ""); 
    $balance = 0; // Initialize the balance for this section... 
    foreach ($combinedResults[$groupKey] as $item) { 
    printf($fmt, $item['doc_date'], $item['descs'], $item['debit'], $item['credit'], ""); 
    $balance += $item['debit']; 
    } 
    printf($fmt, "", "", "", "", $balance); // Print the balance. 
} 

print "</table>\n"; 

我很想知道它是否有效。 :)

請注意,我沒有考慮你的「colspan」;我懷疑你應該在你試圖將它構建成實際的佈局之前先解決你的邏輯問題。

+0

嗨ghoti,感謝您的答覆。我認爲它的工作,但它只需要一點點調整,因爲它根據項目數量循環或顯示多個結果。這裏是示例輸出http://www.freeimagehosting.net/xx7bc – 2012-04-29 17:31:58

+0

嗨ghoti,它再次。它的工作現在,非常感謝您的時間和幫助..非常感謝。 – 2012-04-29 18:21:43

+0

沒問題。感謝您的支持。 – ghoti 2012-05-01 18:26:54

2

你可以改變的東西你的SQL語句就像

SELECT name, doc_date, descs, debit, credit, SUM(debit) AS sum FROM statement WHERE member_id = $member_id ORDER BY doc_date 

然後你可以

<?php echo $item['sum']; ?> 

打印您可能還需要看看PDOprepared statements代替mysql_功能。

+0

hi J-P,查詢結果只返回1項。任何想法? – 2012-04-29 17:33:24