help : SUM Values which have same ID

A

Anonymous

Guest
First of all, forget the nonsense with mysql_numrows() and those bizarre mysql_result() functions and just use mysql_fetch_assoc() and a while() loop like everyone else. Check out the example code at mysql_fetch_assoc(). So much simpler. Where'd you get this code anyway?

Now then, to get the sum, either you can use MySQL's SUM() function (look it up) and an additional query, or you can just use PHP and create a variable to keep a running sum that you update on each pass of the while() loop with whatever the value of computersPrice is:
PHP:
<?php
$sql = "YOUR QUERY GOES HERE";

$result = mysql_query($sql);

/* toss in some error-checking code here */

// here's your counter
$price_total = 0;

echo "<table>\n"; // start your table here
while ($row = mysql_fetch_assoc($result)) {
   /* output your table rows here */
   echo "\t<tr>\n";
   echo "\t\t<td>" . $row['lastName'] . "</td>\n";
   /* and so on... */
   echo "\t</tr>\n";

   // update your running total
   $price_total += $row['computersPrice'];
}
echo "</table>\n"; // end your table

echo $price_total; // the total price
?>
 
Back
Top