twilitegxa Posted September 13, 2009 Share Posted September 13, 2009 How can I add the total price for my shopping cart items? I am not sure how to write the statement. Can anyone help? Here is my shopping cart page: showcart.php: <?php session_start(); include("connect_db.php"); $PHPSESSID = session_id(); $display_block = "<h1>Your Shopping Cart</h1>"; //check for cart items based on user id $PHPSESSID = 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 = '$PHPSESSID'"; $get_cart_res = mysql_query($get_cart) or die(mysql_error()); if (mysql_num_rows($get_cart_res) < 1) { //print message $display_block .= "<p>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 cellpadding=3 cellspacing=2 border=1 width=98%> <tr> <th>Title</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 = 'total price'; $total_price = sprintf($item_price * $item_qty); $display_block .= "<tr> <td align=center>$item_title <br></td> <td align=center>$item_price <br></td> <td align=center>$item_qty <br></td> <td align=center>$total_price S</td> <td align=center><a href=\"removefromcart.php?id=$id\">remove</a></td> </tr>"; } $display_block .= "<tr> <td colspan=4 align=right><b>Total:</b></td> <td>$total</td></table>"; } ?> <html> <head> <title>My Store</title> </head> <body> <?php print $display_block; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/174117-solved-shopping-cart-total/ Share on other sites More sharing options...
cbolson Posted September 13, 2009 Share Posted September 13, 2009 Before you start looping through the results set a variable to 0 like this: $cart_total=0; Then, within your loop add the item total to this variable: $cart_total=$cart_total+$total_price; Chris Link to comment https://forums.phpfreaks.com/topic/174117-solved-shopping-cart-total/#findComment-917831 Share on other sites More sharing options...
twilitegxa Posted September 13, 2009 Author Share Posted September 13, 2009 Thank you so much! That seems to work perfectly! Link to comment https://forums.phpfreaks.com/topic/174117-solved-shopping-cart-total/#findComment-917836 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.