MySQL Forums
Forum List  »  PHP

Help totaling a column
Posted by: Kevin Boaz
Date: April 20, 2006 11:46PM

I have an inventory database and I am trying to total up 3 of the columns. I'm sure that it has been asked how to do this in the past many times, but please, would somebody help me? Here is my code followd by the results:

<?

echo ("<H2>Inventory For Selected Items")."\n";

$link = mysql_connect("localhost","xxxxxxxxxxxxxxx","xxxxxxxxxxxxxxxxxx") or die("Could not connect");
print "<br>"."\n";
mysql_select_db("invtest") or die("Could not select database");
$q_total = 0;
$p_total = 0;
$ep_total = 0;
$query = "SELECT Item_Number, Description, Quanity, Price, Extend_Price FROM inventory where catagory=7";
$result = mysql_query($query) or die("Query Failed");
print "<table border=3 cellpadding=10 cellspacing=1>\n";
print "<tr align=center valign=center>";
print "<td>Item Number</td>";
print "<td>Description</td>";
print "<td>Qty</td>";
print "<td>Price</td>";
print "<td>Total</td>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
print "\t<tr>\n";
foreach ($line as $col_value) {
print "\t\t<td>$col_value</td>\n";
}

print "\t</tr>\n";
}

$query = "SELECT Quanity FROM inventory";
$result = mysql_query($query);
while ($line = mysql_fetch_array($results, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
$q_total = $q_total + $col_value;
}
}
$query = "SELECT Price FROM inventory";
$result = mysql_query($query);
while ($line = mysql_fetch_array($results, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
$p_total = $p_total + $col_value;
}
}

$query = "SELECT Extened_Price FROM inventory";
$result = mysql_query($query);
while ($line = mysql_fetch_array($results, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
$ep_total = $ep_total + $col_value;
}
}

print "\t<tr><td></td><td>Totals</td>\n";
print "\t<td>$q_total</td><td>$p_total</td><td>$ep_total</td></tr>\n";
print "\t</table>\n";


return $data;
mysql_free_result($result);
mysql_close($link);
?>


And the results screen looks like this, but in a table:

Inventory For Selected Items
Item Number Description Qty Price Total
M8003 0 1 12.00 12.00
M8020 0 0 15.50 0.00
M8019 0 1 12.00 12.00
M8018 0 1 15.50 15.50
M8016 0 0 15.50 0.00
M8014 0 1 15.50 15.50
M8006 0 1 12.00 12.00
M8001 0 1 12.00 12.00
M8002 0 17 1.50 25.50
M8005 0 9 1.50 13.50
M8013 0 1 12.00 12.00
M8010 0 0 15.50 0.00
M8009-2 0 2 3.50 7.00
M8012 0 1 15.50 15.50
M8011 0 1 15.50 15.50
M8015 0 0 12.50 0.00
Totals 0 0 0

Options: ReplyQuote


Subject
Written By
Posted
Help totaling a column
April 20, 2006 11:46PM
April 21, 2006 09:21AM
April 22, 2006 03:35PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.