2012-04-25 21 views
5

Ktoś tutaj może mi pomóc rozwiązać ten problem? Chcę tylko uzyskać sumę lub sumę określonej kolumny, proszę odnieść się do obrazu poniżej. Próbowałem posortować to na 2 dni, ale nie mogę mieć szczęścia, Mam nadzieję, że każdy może mi w tym pomóc. Doceniam to tak bardzo, a dzięki w awansem .suma kolumn tablicy

Oto Przykładowy obraz 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> 

Odpowiedz

1

Mam refactored kodu w oparciu o to, co widzę w nim, i dodał, kalkulator równowagi, ale nie faktycznie przetestowane.

<?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"; 

Jestem zainteresowany, aby wiedzieć, czy to działa. :)

Pamiętaj, że nie uwzględniłem twojego "colspanu"; Podejrzewam, że powinieneś zadowolić się logiką, zanim spróbujesz zbudować ją w rzeczywistym układzie.

+0

hi ghoti, dzięki za odpowiedź. Myślę, że działa, ale po prostu wymaga niewielkiej korekty, ponieważ pętle lub wyświetla wiele wyników w zależności od liczby elementów. tutaj jest przykładowy wynik http://www.freeimagehosting.net/xx7bc –

+0

hi ghoti, to znowu ja. DZIAŁA JUŻ TERAZ, dziękuję bardzo za poświęcony czas i pomoc. Doceń tak wiele. –

+0

Bez problemu. Dzięki za przegrany. – ghoti

2

możesz zmienić swoje oświadczenie SQL z czymś jak

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

następnie wydrukować go z

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

kupili także do spojrzeć na PDO i prepared statements, które zastępują funkcje mysql_.

+0

cześć J-P, wyniki zapytania zwracają tylko 1 pozycję. dowolny pomysł? –

Powiązane problemy