Sorry, Jay, but that's a horrible method. You could just run one query with
a GROUP BY clause to get the sum for each invoiceID, instead of running
multiple queries like you've mentioned...
$query = "SELECT invoiceid, SUM(partpaidamount) AS partpaid FROM $tb_name
GROUP BY invoiceid";
$rs = mysql_query($query) or die(mysql_error());
while($r = mysql_fetch_assoc($rs))
{ echo "Invoice: {$r['invoiceid']}, SUM: {$r['partpaid']}"; }
---John Holmes...
PS: Sorry for the top post, but I'm at work and outlook express sucks. :)
----- Original Message -----
From: "Jay Blanchard" <[EMAIL PROTECTED]>
To: "Ben C." <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Tuesday, August 05, 2003 8:29 AM
Subject: RE: [PHP] Sum a column of values from a MySQL query
[snip]
1) The code returns the sum of a partially paid invoice. The individual
invoice is 'partpaid'. WORKS...NO PROBLEM
2) The while() then return a list of partially paid invoices which is
$invoicepartpaid. WORKS..NO PROBLEM
3) I then want to add the list of partially paid invoices
($invoicepartpaid)
together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER.
[/snip]
Ben...do you want to add together invoices where each ID is different?
Is that right? If so I have the answer! You need to loop through the
ID's which you are not doing....I have taken your code and added to
it....(and made changes for clarity in the e-amil)
//get invoice id's
$sql_1 = "SELECT DISTINCT(invoiceid) as inid FROM $tb_name ";
$result_1 = mysql_query($sql_1, $connection) or die(mysql_error());
while($idinvoice = mysql_fetch_array($result_1)){
// get partial paid for THIS invoice
$sql_2 = "SELECT SUM(partpaidamount) as partpaid ";
$sql_2 .= "FROM $tb_name ";
$sql_2 .= "WHERE invoiceid = '" . $idinvoice['inid'] . "' ";
$result_2 = @mysql_query($sql_2,$connection) or die(mysql_error());
//you will only be returning ONE row, so no need for while loop
$row = mysql_fetch_array($result_2);
$invoicepartpaid = $row['partpaid'];
$total = $total + $invoicepartpaid;
} // loop to NEXT invoice
print(number_format($total, 2, '', ','). "\n");
Does this make sense? You must loop through each ID and query for that
ID's partial amount.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php