Jump to content

HELP about my calculation


AeonE

Recommended Posts

guys, I'm having a problem how to calculate the grandTotal. heres the case :

[img src=\"http://www.geocities.com/d3private/a.jpg\" border=\"0\" alt=\"IPB Image\" /]

see that picture above? a part of that the grand Total is missing, and I dont know how to calculate that
because I'm using the loop command , and dont have any specific for rows

btw heres the code for displaying above

[quote]

$query = "SELECT * FROM cart INNER JOIN products ON cart.pId = products.pId where username = $_SESSION[username] ";
$result = mysql_query($query) or die ("Query Error on listing MyCart");
$total=mysql_num_rows($result);
for($n=0; $n<$total; $n++)
{
while($row=mysql_fetch_array($result) )
{
if($row > 1)
{
$a += 1;

?>

<tr>
<td align="center" width="30" height="26"><?php echo $a ?></td>
<td align="center" width="50" height="26"><img src="../../Image/<?php echo $row[pImage]; ?>" width="50" height="50"></td>
<td align="center" width="100" height="26"><?php echo $row[pName] ?></td>
<td align="center" width="25" height="26"><input type=text name=quantity size= "1" value="<?php echo $row[cQuantity] ?>"></td>
<td align="center" width="75" height="26"><b>$</b> <? echo $row[cartPrice] ?></td>
<?php $total1 = $row[cQuantity] * $row[cartPrice]; ?>
<td align="center" width="75" height="26"><?php echo $total1 ?></td>
<td align="center" width="30" height="26"> </td>
</tr>
</table>

[/quote]

I use <?php $total1 = $row[cQuantity] * $row[cartPrice]; ?> for the calculating [quantity * price ea],
and for the grandTotal <?php $gTotal = ...... ?>....?,
how to calculate the grand total.... HELPPPP
Link to comment
Share on other sites

What you'll have to do is calculate your gTotal variable everytime it loops through the while loop. This si what you'll want to do:
[code]$gTotal += $row['cQuantity'] * $row['cartPrice'];[/code]This is the same as:
[code]$gTotal = $gTotoal + $row['cQuantity'] * $row['cartPrice'];[/code]but shorter.
So here is you new code:
[code]<?php
session_start();
include("../../Files/Database.php");
?>
<html>
<head>
<title>MyCart Information</title>
</head>

<body>
<form method=post>
<p align=left> Cart Information for <b><?php echo $_SESSION['username']?></b>,</p>

<table border="1" width="450" id="table1">
<tr>
<td align="center" width="30"> </td>
<td align="center" width="578" colspan="5"><b>My Cart Index</b></td>
<td align="center" width="30" height="26"> </td>

</tr>
<tr>
<td align="center" width="30"><b>No</b></td>
<td align="center" width="50"><b>Image</b></td>
<td align="center" width="100"><b>Product Name</b></td>
<td align="center" width="25"><b>Q</b></td>
<td align="center" width="75"><b>Price ea</b></td>
<td align="center" width="75" height="26"><b>Total</b></td>
<td align="center" width="30"> </td>
</tr>

<?php
if($_SESSION['username'] == "")
{
    die ("Please login 1st");
}
else
{

    //$query = "Select * from cart where username = $_SESSION[username] ";
    $query = "SELECT * FROM cart INNER JOIN products ON cart.pId = products.pId where username = $_SESSION[username] ";
    $result = mysql_query($query) or die ("Query Error on listing MyCart");

    $gTotal = "";

    while($row=mysql_fetch_array($result) )
    {
        if($row > 1)
        {
            $a += 1;

            $gTotal += $row[cQuantity] * $row[cartPrice];
?>
<tr>
<td align="center" width="30" height="26"><?php echo $a ?></td>
<td align="center" width="50" height="26"><img src="../../Image/<?php echo $row[pImage]; ?>" width="50" height="50"></td>
<td align="center" width="100" height="26"><?php echo $row[pName] ?></td>
<td align="center" width="25" height="26"><input type=text name=quantity size= "1" value="<?php echo $row[cQuantity] ?>"></td>
<td align="center" width="75" height="26"><b>$</b> <? echo $row[cartPrice] ?></td>
<td align="center" width="75" height="26"><?php echo $total1 ?></td>
<td align="center" width="30" height="26"> </td>
</tr>
<?php
        }
        else
        {
            echo "Retriving data failed!!";
        }
    }
}
?>
<tr>
<td align="center" width="30" height="26"> </td>
<td align="center" width="175" height="26" colspan="3"> </td>
<td align="center" width="75" height="26"><b>Total</b></td>
<td align="center" width="75" height="26"><?php echo $gTotal?></td>
<td align="center" width="30" height="26"> </td>
</tr>
</table>

<table border="0" width="450" id="table2">
<tr>
<td align="center"> </td>
<td width="50" align="center"><input type=button name="updateCart" value="Update"></td>
<td width="169" align="center"><input type=button name="checkOut" value="Check Out"></td>
</tr>
</table>

<p align=left><b>  Note : Please press Update first before Checkout, if you made any changes.</b></p>
</form>
</body>
</html>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.