AeonE Posted March 31, 2006 Share Posted March 31, 2006 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 thatbecause I'm using the loop command , and dont have any specific for rowsbtw 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 Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 31, 2006 Share Posted March 31, 2006 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]<?phpsession_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><?phpif($_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] Quote Link to comment Share on other sites More sharing options...
AeonE Posted March 31, 2006 Author Share Posted March 31, 2006 yay man it works, using $gTotal += $row[cQuantity] * $row[cartPrice];THANKSSSS alot man, u been a great help!!thx again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.