Jump to content

Archived

This topic is now archived and is closed to further replies.

dhrups

need to add up rows

Recommended Posts

Hi, i am trying to add up the total for my code.
i have used the following code to multiply price with quantity and display it as total. I am not storing the values in total on the database.
I need to add the total up. ie. i have 2 rows with to different totals, for e.g total
200
200
final Total:
i want ti fill in that final total.

<?php //start of php coding and running mySQL queries.

$result = mysql_query("SELECT * FROM accessories ORDER BY category");
$row = mysql_numrows($result);



$i=0;
while ($i < $row)

{
$Category = mysql_result($result,$i,"category");
$Description = mysql_result($result,$i,"description");
$Price = mysql_result($result,$i,"price");
$Quantity = mysql_result($result,$i,"quantity");


$Total = ($Price * $Quantity);



?>

<tr>
<td width="154" align="center" style="border-style: solid; border-width: 1"><b><font face="Arial, Helvetica, sans-serif"><? echo $Category; ?></font>&nbsp;</td>
<td width="321" align="left" style="border-style: solid; border-width: 1"><b><font face="Arial, Helvetica, sans-serif"><? echo $Description; ?></font></b>&nbsp;</td>
<td width="157" align="center" style="border-style: solid; border-width: 1"><font face="Arial, Helvetica, sans-serif"><? echo number_format($Price, 2); ?></font>&nbsp;</td>
<td width="104" align="center" style="border-style: solid; border-width: 1"><font face="Arial, Helvetica, sans-serif"><? echo $Quantity; ?></font>&nbsp;</td>
<td width="159" align="center" style="border-style: solid; border-width: 1"><b><font face="Arial, Helvetica, sans-serif"><? echo number_format($Total, 2); ?></font></b>&nbsp;</td>
</tr>


<?
$i++;
}
?>

</tr>

Share this post


Link to post
Share on other sites
try this out

[code]<?php //start of php coding and running mySQL queries.
$Total = 0;
$result = mysql_query("SELECT * FROM accessories ORDER BY category");
$row = mysql_num_rows($result);

if($row > 1){
while($r = mysql_fetch_array($result)){
// This extract the fields and puts them as variables so you can use $price instead or $r['price']
extract($r);
// Create the subtotal
$SubTotal = ($price * $quantity);
?>
<tr>
<td width="154" align="center" style="border-style: solid; border-width: 1"><b><font face="Arial, Helvetica, sans-serif"><? echo $category; ?></font> </td>
<td width="321" align="left" style="border-style: solid; border-width: 1"><b><font face="Arial, Helvetica, sans-serif"><? echo $description; ?></font></b> </td>
<td width="157" align="center" style="border-style: solid; border-width: 1"><font face="Arial, Helvetica, sans-serif"><? echo number_format($price, 2); ?></font> </td>
<td width="104" align="center" style="border-style: solid; border-width: 1"><font face="Arial, Helvetica, sans-serif"><? echo $quantity; ?></font> </td>
<td width="159" align="center" style="border-style: solid; border-width: 1"><b><font face="Arial, Helvetica, sans-serif"><? echo number_format($SubTotal, 2); ?></font></b> </td>
</tr>
<?
// start adding up the subtotals, each time it loops it will add the $SubTotal to the running $Total
$Total += $SubTotal;
}
}
?>
</tr>
<tr>
<td colspan=5 align="right" style="border-style: solid; border-width: 1"><b><font face="Arial, Helvetica, sans-serif"><? echo number_format($Total, 2); ?></font></b> </td>
</tr>
</table>[/code]

Ray

Share this post


Link to post
Share on other sites
Didn't you ask this once before or am I remembering a very similar question?

Let's tighten up your code and try to fix your problem:
[code]<?php
$query = "SELECT * FROM accessories ORDER BY category";
$result = mysql_query($query) or die ("Problem with the query: $query <br>" . mysql_error());
// $row = mysql_numrows($result); // don't need this line, besides the function is called mysql_num_rows()
$Total = 0;
while ($row = mysql_fetch_assoc($result)) { // loop through the retrieved rows
{
     $Category = $_row['category'];
     $Description = $_row['description'];
     $Price = $row['price'];
     $Quantity = $row['quantity'];
     $Total += $Price * $Quantity; // The += operator says to add the result to the variable on the left side of the equation. It is equivalent to $Total = $Total + ($Price * $Quantity);
?>

<tr>
Helvetica, sans-serif">
<td width="321" align="left" style="border-style: solid; border-width: 1"><b><font face="Arial, Helvetica, sans-serif"><? echo $Description; ?></font></b>&nbsp;</td>
<td width="157" align="center" style="border-style: solid; border-width: 1"><font face="Arial, Helvetica, sans-serif"><? echo number_format($Price, 2); ?></font>&nbsp;</td>
<td width="104" align="center" style="border-style: solid; border-width: 1"><font face="Arial, Helvetica, sans-serif"><? echo $Quantity; ?></font>&nbsp;</td>
</tr>
<?
}
?>
<td width="159" align="center" style="border-style: solid; border-width: 1"><b><font face="Arial, Helvetica, sans-serif"><? echo number_format($Total, 2); ?></font></b>&nbsp;</td>
[/code]
I also move the output of the $Total to after your loop.

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.