Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with shopping cart code; calculation 1

Status
Not open for further replies.

rninja

Technical User
Joined
Apr 11, 2001
Messages
381
Location
US
Hello.

I am trying to add a total in a shopping cart script that was premade. Thing is the shopping cart takes from the mysql database and then spits the info into an html table. That table calculates the total of that item however, there is no total sum for all items in the cart.

I want to add those prices into a total. Just so that it is clearer what I am doing, here is a segment of the script:

$display_block .=&quot;<table cellpadding=\&quot;3\&quot; cellspacing=\&quot;2\&quot; border=\&quot;0\&quot; width=\&quot;98%\&quot;><tr>
<th>Title</th>
<th>Price</th>
<th>Qty</th>
<th>Total Price</th>
<th>Action</th>
</tr>&quot;;

while($cart = mysql_fetch_array($get_cart_res)){
$id = $cart['id'];
$item_title = stripslashes($cart['item_title']);
$item_price = $cart['item_price'];
$item_qty = $cart['sel_item_qty'];

$total_price = sprintf(&quot;%.02f&quot;,$item_price * $item_qty);

$display_block .=&quot;<tr>
<td align=\&quot;center\&quot;>$item_title</td>
<td align=\&quot;center\&quot;>\$ $item_price<br></td>
<td align=\&quot;center\&quot;>$item_qty</td>
<td align=\&quot;center\&quot;>\$ $total_price</td>
<td align=\&quot;center\&quot;><a href=\&quot;remove.php?id=$id\&quot;>Remove</a></td>

</tr>&quot;;
}


Thanks in advance for assistance.

Rninja

smlogo.gif

 
I am actually still having problems with it. I am not creating the code properly. Can you give me an example to work with in this particular script?

Rninja

smlogo.gif

 
Pick a name for your aggregation variable.

Just before entering the while loop, set that variable to zero.

Just before the end of the while loop (but inside the loop), set the aggregation variable to be equal to the aggregation variable plus (the item price times the item quantity).

Outside the while loop, output the value in an appropriate place within table subelement tags.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
I did this and it just outputs the last subtotal not the full total with each element in the cart.

Is there anyway to get the subtotals inside that while loop into one grand total?

Rninja

smlogo.gif

 
So what you are saying is that before:

while($cart = mysql_fetch_array($get_cart_res)){
$id = $cart['id'];
$item_title = stripslashes($cart['item_title']);
$item_price = $cart['item_price'];
$item_qty = $cart['sel_item_qty'];

$total_price = sprintf(&quot;%.02f&quot;,$item_price * $item_qty);

$display_block .=&quot;<tr>
<td align=\&quot;center\&quot;>$item_title</td>
<td align=\&quot;center\&quot;>\$ $item_price<br></td>
<td align=\&quot;center\&quot;>$item_qty</td>
<td align=\&quot;center\&quot;>\$ $total_price</td>
<td align=\&quot;center\&quot;><a href=\&quot;remove.php?id=$id\&quot;>Remove</a></td>

</tr>&quot;;
}




I should make another while loop that keeps track of the total? How do I do that? I am not familiar with php more than for edits and certain programming concepts.

Rninja

smlogo.gif

 
What exactly do you want to display where?

The code you have posted will display the item extension (what you're storing in $total_price). If you want to display an order total, you will have to sum all of the calculations for item extensions.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Yes I want to sum that total from each extension as you say. How do I do that? Can you give me an example using my code? I have no idea what to do with that. I tried it in every way I could and no result produced the sum.

Thanks in advance for the patience.

Rninja

smlogo.gif

 
Assuming we're using the newest version of the code you posted....

Just before entering the while loop, I would add:

$grand_total = 0;

Just before the end of the while loop, the last line before the closing brace (&quot;}&quot;), I would add the line:

$grand_total += $total_price;

$grand_total will then have the order total, ready to be displayed.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top