2012-04-25 20 views
5

Chiunque può aiutarmi a risolvere questo problema? Voglio solo ottenere il totale o la somma di determinate colonne, per favore fai riferimento all'immagine qui sotto. Sto provando a ordinare questo per 2 giorni ma non posso ottenere non avere fortuna, Spero che qualcuno mi possa aiutare con questo. Lo apprezzo molto, e grazie in anticipo .sum of array column

Ecco un esempio di immagine 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> 

risposta

1

Ho rifattorizzato il codice in base a ciò che vedo in esso e ho aggiunto un calcolatore di bilanciamento, ma non l'ho ancora testato.

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

Sono interessato a sapere se funziona. :)

Nota che non ho preso indennità per il tuo "colspan"; Sospetto che dovresti accontentarti della tua logica prima di provare a costruirla in un layout reale.

+0

hi ghoti, grazie per la risposta. Penso che funzioni, ma ha solo bisogno di un piccolo aggiustamento perché loop o visualizza più risultati a seconda del numero di elementi. ecco l'output di esempio http://www.freeimagehosting.net/xx7bc –

+0

hi ghoti, è di nuovo me. LAVORA SUBITO, grazie mille per il tuo tempo e il tuo aiuto .. apprezzo tanto. –

+0

Nessun problema. Grazie per l'upvote. – ghoti

2

È possibile modificare l'istruzione SQL con qualcosa di simile

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

È quindi stamparlo con

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

Si potrebbe anche voler dare un'occhiata a PDO e prepared statements che sostituiscono le funzioni mysql_.

+0

hi J-P, i ​​risultati della query restituiscono solo 1 elemento. qualche idea? –