menios Posted December 7, 2007 Share Posted December 7, 2007 I ve managed to setup my supersimple shopping cart it works up to the point of storring the items and quantities the user has selected. Its based on sessions.What i want to do next is when the user hits the procceed button to be prompted to fill a from with his/her details and then store those items in the DB. my knowledge is limited on how to do that. Can i have some help pls? Heres is my code in case it helps. cart.php [hr]<?php // Include MySQL class require_once('inc/mysql.class.php'); // Include database connection require_once('inc/global.inc.php'); // Include functions require_once('inc/functions.inc.php'); // Start the session session_start(); // Process actions $cart = $_SESSION['cart']; $action = $_GET['action']; switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['pd_id']; } else { $cart = $_GET['pd_id']; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['pd_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')) { $pd_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 .= ','.$pd_id; } else { $newcart = $pd_id; } } } } } $cart = $newcart; break; } $_SESSION['cart'] = $cart; ?> <html> <head> <title></title> <link rel="stylesheet" href="css/styles.css"> </head> <body> <div id="contents"> <h1>Your Shopping Cart</h1> <?php echo writeShoppingCart(); echo showCart(); ?> <p><a href="iframe.html" target="display">Continue Shopping</a></p> </div> </body> </html> And Functions.php [hr]<?php function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>Your Cart is Empty</p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<p>Your Cart contains <a href="cart.php">'.count($items).' book'.$s.' </a></p>'; } } function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $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 border="0">'; $output[] = '<tr>'; $output[] = '<th bgcolor=#62aac4><strong>Book</strong></th>'; $output[] = '<th bgcolor=#62aac4>Price</th>'; $output[] = '<th bgcolor=#62aac4>Quantity</th>'; $output[] = '<th bgcolor=#62aac4 colspan=3> </th>'; $output[] = '</tr>'; foreach ($contents as $pd_id=>$qty) { $sql = 'SELECT * FROM tbl_product WHERE pd_id = '.$pd_id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td align=center><strong>'.$pd_name.'</strong></td>'; $output[] = '<td align=center>£'.$pd_price.'</td>'; $output[] = '<td align=right ><input type="text" name="qty'.$pd_id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td align=right><i>£'.($pd_price * $qty).'</i></td>'; $total += $pd_price * $qty; $output[] = '<td align=center><button type="submit">Add</button></td>'; $output[] = '</form>'; $output[] = '<form action="cart.php?action=delete&pd_id='.$pd_id.'" method="post" id="cart">'; $output[] = '<td align=center><button type="submit" class="r">Remove</button></td>'; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p><pre><strong><i> Total: <big></i>£'.$total.'</strong></big></pre></p>'; $output[] = '</form>'; $output[] = '<pre> <button id="checkout" type="submit">Checkout</button></pre>'; } else { $output[] = '<p>Your Cart is empty.</p>'; } return join('',$output); } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 7, 2007 Share Posted December 7, 2007 You need to add a HTML Form to request that info then post it to the database. Quote Link to comment Share on other sites More sharing options...
menios Posted December 7, 2007 Author Share Posted December 7, 2007 I ve created the form also and added <?php $_SESSION['cart'] = $cart;?> so this will import the cart details if i m not mistaken ??? <-- IS this correct? so now i ll need to do the form validation and then insert everything in the database. Quote Link to comment Share on other sites More sharing options...
jsoeurt Posted December 7, 2007 Share Posted December 7, 2007 If you want to get the value of $_SESSION['cart'] and store it in $cart you'll have to do it the other way around: <?php $cart = $_SESSION['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.