Jump to content


Photo

HELP about my calculation


  • Please log in to reply
2 replies to this topic

#1 AeonE

AeonE
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 31 March 2006 - 12:29 PM

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


$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>


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

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 March 2006 - 12:53 PM

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:
$gTotal += $row['cQuantity'] * $row['cartPrice'];
This is the same as:
$gTotal = $gTotoal + $row['cQuantity'] * $row['cartPrice'];
but shorter.
So here is you new 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>


#3 AeonE

AeonE
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 31 March 2006 - 01:02 PM

yay man it works, using $gTotal += $row[cQuantity] * $row[cartPrice];


THANKSSSS alot man, u been a great help!!

thx again




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users