ballhogjoni Posted July 12, 2007 Share Posted July 12, 2007 I am going to explain this the best that I can. The following block of code is from my shopping cart. On any product page my user can select if there product is for a boy or girl and then when they submit the form the product id gets passed in the url and the boy or girl attribute is passed with post to cart.php. The issue I am runnign into is displaying whether the product is for a boy or a girl on the "view shopping cart page" becasue the user can have multiple products that can be for a boy or girl and then products that are niether. I use a function called showcart() that displays the cart (its posted below as well). If you have questions please ask. Thanks in advance. cart.php <?php require_once('inc/functions.inc.php'); $cart = $_SESSION['cart']; $boyGirl = $_SESSION['boyGirl']; $action = $_GET['action']; $id = $_POST['tier']; $is_boy = $_POST['boyGirl']; $boyGirl = $_SESSION['boyGirl']; switch ($action) { case 'add': if ($cart) { $cart .= ','.$id; } else { $cart = $id; } if ($boyGirl) { $boyGirl .= ','.$is_boy; } else { $boyGirl = $is_boy; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['id'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $newcart; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; break; } $_SESSION['boyGirl'] = $boyGirl; $_SESSION['cart'] = $cart; ?> <p class="style2">Your Shopping Cart</p> <?php echo writeShoppingCart(); echo showCart(); ?> showcart() <?php function showCart() { //global $db; $cart = $_SESSION['cart']; $boyGirl = $_SESSION['boyGirl']; if ($cart && $boyGirl) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table width="500px" style="border:1px solid #EEEEEE;">'; $output[] = '<tr>'; $output[] = '<td>'; $output[] = '<table align="center">'; $output[] = '<tr>'; $output[] = '<td align="center" class="style1">Item</td>'; $output[] = '<td align="center" class="style1">Qty</td>'; $output[] = '<td align="center" class="style1">Price</td>'; $output[] = '<td align="center" class="style1">Sub Total</td>'; $output[] = '</tr>'; foreach ($contents as $id=>$qty) { $username="xxxxx"; $password="xxxx"; $database="xxxxx"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = 'SELECT * FROM products WHERE id = '.$id; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); /*$sql = 'SELECT * FROM products WHERE id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row);*/ $output[] = '<td class="style4" align="center">'.$row['title'].' '.$boyGirl.'</td>'; $output[] = '<td align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td class="style4" align="center">$'.$row['price'].'</td>'; $output[] = '<td class="style4" align="center">$'.($row['price'] * $qty).'</td>'; $output[] = '<td align="center"><a href="cart.php?action=delete&id='.$id.'" class="r"><img src="http://xxxx/images/delete.jpg" alt="delete the diaper cake product" border="0" width="22" height="25"></a></td>'; $total += $row['price'] * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<table align="center" width="500px">'; $output[] = '<tr>'; $output[] = '<td>'; $output[] = '</td>'; $output[] = '<td align="right">'; $output[] = '<button type="submit">Update Qty</button>'; $output[] = '</td>'; $output[] = '</tr>'; $output[] = '<tr>'; $output[] = '<td>'; $output[] = '</td>'; $output[] = '<td align="right" style="font-size:large">'; $output[] = 'Sub Total:'; $output[] = '</td>'; $output[] = '<td style="font-size:medium; font-weight:bold; text-align:center;">$'.$total; $output[] = '</td>'; $output[] = '</tr>'; $output[] = '</table>'; $output[] = '</form>'; $output[] = '<table width="500px" align="center">'; $output[] = '<tr>'; $output[] = '<td>'; $output[] = '<table align="center">'; $output[] = '<tr>'; $output[] = '<td align="center" class="style1"><a href="http://xxx.com"><font size="2">Continue Shopping</font></a></td>'; $output[] = '<td align="right">'; $output[] = '<form method="POST" action="https://checkout.google.com/cws/v2/Merchant/xxxxx/checkoutForm" accept-charset="utf-8">'; foreach ($contents as $id=>$qty) { /*$sql = 'SELECT * FROM products WHERE id = '.$id.' LIMIT = boy AND girl'; $result = $db->query($sql); $row = $result->fetch(); extract($row);*/ if ($boy_girl == 1) { $boygirl = 'Boy Cake'; $output[] = '<input type="hidden" name="shopping-cart.items.item-'.$id.'.merchant-private-item-data" value="'.$boygirl.'">'; } elseif ($boy_girl == 2) { $boygirl = 'Girl Cake'; $output[] = '<input type="hidden" name="shopping-cart.items.item-'.$id.'.merchant-private-item-data" value="'.$boygirl.'">'; } $output[] = '<input type="hidden" name="item_name_'.$id.'" value="'.$title.' '.$boygirl.'"/>'; $output[] = '<input type="hidden" name="item_description_'.$id.'" value="A completly handmade diaper cake with elegant decorations"/>'; $output[] = '<input type="hidden" name="item_quantity_'.$id.'" value="'.$qty.'"/>'; $output[] = '<input type="hidden" name="item_price_'.$id.'" value="'.$price.'"/>'; } $output[] = ' <input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.edit-cart-url" value="http://xxxx.com/cart.php"> <input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.continue-shopping-url" value="http://xxxx.com"> <input type="hidden" name="ship_method_name_1" value="UPS Ground"/> <input type="hidden" name="ship_method_price_1" value="16.95"/> <input type="hidden" name="ship_method_us_area_1" value="FULL_50_STATES"/> <input type="hidden" name="ship_method_name_2" value="UPS Priority"/> <input type="hidden" name="ship_method_price_2" value="29.99"/> <input type="hidden" name="ship_method_us_area_2" value="FULL_50_STATES"/> <input type="hidden" name="ship_method_name_3" value="UPS Express Overnight"/> <input type="hidden" name="ship_method_price_3" value="45.99"/> <input type="hidden" name="ship_method_us_area_3" value="FULL_50_STATES"/> <input type="hidden" name="ship_method_name_4" value="Local Delivery"/> <input type="hidden" name="ship_method_price_4" value="10.00"/> <input type="hidden" name="ship_method_us_area_4" value="FULL_50_STATES"/> <input type="hidden" name="ship_method_name_5" value="Pickup"/> <input type="hidden" name="ship_method_price_5" value="0.00"/> <input type="hidden" name="ship_method_us_area_5" value="FULL_50_STATES"/> <input type="hidden" name="checkout-flow-support.merchant-checkout-flow-support.shipping-methods.flat-rate-shipping.shipping-restrictions.allowed-areas.world-area" value="http://xxxx.com/cart.php"> <input type="hidden" name="_charset_"/> <input type="image" name="Google Checkout" alt="Fast checkout through Google" src="https://checkout.google.com/buttons/checkout.gif?merchant_id=xxxx&w=180&h=46&style=white&variant=text&loc=en_US"/> </form>'; $output[] = '</td>'; $output[] = '</tr>'; $output[] = '</table>'; $output[] = '</td>'; $output[] = '</tr>'; $output[] = '</table>'; } ?> Quote Link to comment Share on other sites More sharing options...
shamilton Posted July 12, 2007 Share Posted July 12, 2007 Why don't you have a cache table setup for the shopping cart? Store each users session ID into a table, and track their items by Session ID, Item Number, [Any other things like Boy/Girl]. This is good if you would like the users to be able to come back and not lose everything in their shopping cart. 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.