Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100



Go Back   PHP-Editors > Programming Help > PHP Programming Help

PHP Programming Help Post any question relating to PHP Programming here and hopefully someone can help.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 2005-08-05, 08:54 AM
Junior Member
 
Join Date: Aug 2005
Posts: 3
jvandal
Default

I am using some code and wanted to add a total and display it after the table is displayed. I have tried several places but have not been able to understand the coding to have it I also have the choices to go to other scripts that is also displayed before the tables
Here is the code.
Thanks for any help
jvandal
---------
<?php
session_start();

//connect to database
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("mystoredb",$conn) or die(mysql_error());

$display_block = "<h1>Your MatchBrush Shopping Cart</h1>";

//check for cart items based on user session id
$get_cart = "select st.id, si.item_title, si.item_price, st.sel_item_qty, st.sel_item_size, st.sel_item_color from store_shoppertrack as st left join store_items as si on si.id = st.sel_item_id where session_id = '$_COOKIE[PHPSESSID]'";
$get_cart_res = mysql_query($get_cart) or die(mysql_error());

if (mysql_num_rows($get_cart_res) < 1) {
//print message
$display_block .= "

You have no items in your cart.
Please <a href=\"seestore.php\">continue to shop</a>!</p>";

} else {
//get info and build cart display
$display_block .= "
<table celpadding=3 cellspacing=2 border=1 width=98%>
<tr>
<th>Title</th>
<th>Size</th>
<th>Color</th>
<th>Price</th>
<th>Qty</th>
<th>Total Price</th>
<th>Action</th>
</tr>";

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'];
$item_color = $cart['sel_item_color'];
$item_size = $cart['sel_item_size'];
$total_price = sprintf("%.02f", $item_price * $item_qty);
######## this is the total sttement i have added
$_final_total = $_final_total + $total_price;

$display_block .= "<tr>
<td align=center>$item_title
</td>
<td align=center>$item_size
</td>
<td align=center>$item_color
</td>
<td align=center>\$ $item_price
</td>
<td align=center>$item_qty
</td>
<td align=center>\$ $total_price</td>
<td align=center><a href=\"removefromcart.php?id=$id\">remove</a></td>
</tr>";
} }
#display_block .= " <tr> <td align=center> \ </td></tr>";
####### this is the statement I added to display the grand total
echo (" Total amount for this purchase is $".sprintf("%.02f", $_final_total));




?>
<HTML>
<HEAD>
<TITLE>My Store</TITLE>
</HEAD>
<BODY>
<!--<ahref="next.php?<?php echo(SID); ?>"<Next Page</a> -->
<ahref="next.php?"<Next Page</a>
<hr>
<!--<? echo( session_id()); ?> -->
<?php echo $display_block; ?>
######## this is the transfer statement I have add to


Your entry has been added. Would you like to
add another item?
<Would you like to Check Out???</p>"

</BODY>
</HTML>
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 2005-08-07, 12:57 PM
Senior Member
 
Join Date: Dec 2004
Posts: 199
strasm is an unknown quantity at this point
Default

Put an accumulator in your while loop. I am not sure exactly what you were getting, but you dont want to use sprintf to convert a number to a string. use number_format($number, 2) to get a number into #.## format instead.

if things are still not working post back with some results.

Code:
 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'];
  $item_color = $cart['sel_item_color'];
  $item_size = $cart['sel_item_size'];
  $total_price = sprintf("%.02f", $item_price * $item_qty);
  ######## this is the total sttement i have added
  $_final_total += number_format($total_price * $item_qty, 2);

  $display_block .= "<tr>
    <td align=center>$item_title <br></td>
    <td align=center>$item_size <br></td>
    <td align=center>$item_color <br></td>
    <td align=center>\$ $item_price <br></td>
    <td align=center>$item_qty <br></td>
    <td align=center>\$ $total_price</td>
    <td align=center><a href=\"removefromcart.php?id=$id\">remove</a></td>
    </tr>";
}
Reply With Quote
  #3 (permalink)  
Old 2005-08-10, 02:30 AM
Junior Member
 
Join Date: Aug 2005
Posts: 1
gunman
Default

If results from your query are correct then grand total also have to be ok.
__________________
http://freepgs.com/romeo5/
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.5 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.5 (a popular PHP IDE) and NuCoder 2.0 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:06 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.