mrooks1984 Posted March 3, 2012 Share Posted March 3, 2012 hello all, hoping someone can help, i am still pretty new to php and i am stuck on creating a php cart. the code i have below is working, but the problem i am having is i need a store in the session array i am struggling to get my head around it so hoping someone can help here. i am hoping to have $product_id (which is currently in) $option1,2,3 and $text (for a textarea box) heres my function file function get_product() { echo "Hello"; } function add_product() { $product_id = $_POST['product_id']; if ($product_id == 'NULL') { echo "No Product Selected!"; } if (isset($_SESSION['cart'][$product_id])) { $_SESSION['cart'][$product_id]++; echo "<script>window.location='product.php'</script>"; }else{ $_SESSION['cart'][$product_id] = 1; echo "<script>window.location='product.php'</script>"; } } function increase_product() { $product_id = $_GET['product_id']; $_SESSION['cart'][$product_id]++; echo "<script>window.location='cart.php'</script>"; } function decrease_product() { $product_id = $_GET['product_id']; $_SESSION['cart'][$product_id]--; echo "<script>window.location='cart.php'</script>"; } function empty_cart() { unset($_SESSION['cart']); echo "<script>window.location='cart.php'</script>"; } } my product page (currently with out the extra form items (option1,2,3 etc) include '_class/cart.php'; $core->session($mode = 'start'); if(isset($_POST['buy'])) { $cart->add_product(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Product</title> </head> <body> <?php $core->navbar();?> <form method="post" action=""> <input name="product_id" type="hidden" value="Coffee" /> <h1>Cup Of Coffee</h1> <p>Buy me you know you want too!</p> <input name="buy" type="submit" value="Add To Basket" id="buy" /> </form> <form method="post" action=""> <input name="product_id" type="hidden" value="Biscuits" /> <h1>Barrel Of Biscuits</h1> <p>When Coffee Just Gives You The Munchies!</p> <input name="buy" type="submit" value="Add To Basket" id="buy" /> </form> </body> </html> my include cart file if (isset($_SESSION['cart'])) {//Session Cart foreach($_SESSION['cart'] as $product_id => $quantity){//Session Array if($quantity < 1){//Quantity Less Then 1 unset($_SESSION['cart'][$product_id]); echo "<script>window.location='cart.php'</script>"; }//End Quantity Less Then 1 if(!$_SESSION['cart']){//Cart Array Empty unset($_SESSION['cart']); echo "<script>window.location='cart.php'</script>"; }//End Cart Array Empty if ($product_id != NULL){//Show Products echo '<h1>' . $product_id . '</h1>'; echo "Quantity <br>"; ?><a href="cart.php?product_id=<?php echo $product_id; ?>&option=increase">+</a> <?php echo $quantity; ?> <a href="cart.php?product_id=<?php echo $product_id; ?>&option=decrease">-</a><?php }//End Show Products }//End Session Array require 'library/paypal.php'; }else{ echo "Your Basket Is Empty"; }//End Session Cart and my actual cart file include '_class/cart.php'; $core->session($mode = 'start'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Cart</title> </head> <body> <?php $core->navbar(); if (isset($_GET['product_id']['option'])) { $product_id = $_GET['product_id']; $option = $_GET['option']; if ($option == 'decrease'){ $cart->decrease_product(); } if ($option == 'increase'){ $cart->increase_product(); } }elseif (isset($_GET['option'])){ $option = $_GET['option']; if ($option == 'empty'){ $cart->empty_cart(); } }else{ require 'library/cart.php'; } ?> <p><a href="cart.php?option=empty">Empty Cart</a></p> </body> </html> please explain answers or exmples as clear as possible thanks everyone Quote Link to comment https://forums.phpfreaks.com/topic/258172-_sessioncart-help/ Share on other sites More sharing options...
Pikachu2000 Posted March 3, 2012 Share Posted March 3, 2012 How about posting the code in . . . or . . . tags? Quote Link to comment https://forums.phpfreaks.com/topic/258172-_sessioncart-help/#findComment-1323503 Share on other sites More sharing options...
mrooks1984 Posted March 3, 2012 Author Share Posted March 3, 2012 hi i noticed afterwords but for some reason i cant edit my own posts which seems silly? anyway i repost it: heres my function file: <?php //Cart Class File class Cart extends Core { function get_product() { echo "Hello"; } function add_product() { $product_id = $_POST['product_id']; if ($product_id == 'NULL') { echo "No Product Selected!"; } if (isset($_SESSION['cart'][$product_id])) { $_SESSION['cart'][$product_id]++; echo "<script>window.location='product.php'</script>"; }else{ $_SESSION['cart'][$product_id] = 1; echo "<script>window.location='product.php'</script>"; } } function increase_product() { $product_id = $_GET['product_id']; $_SESSION['cart'][$product_id]++; echo "<script>window.location='cart.php'</script>"; } function decrease_product() { $product_id = $_GET['product_id']; $_SESSION['cart'][$product_id]--; echo "<script>window.location='cart.php'</script>"; } function empty_cart() { unset($_SESSION['cart']); echo "<script>window.location='cart.php'</script>"; } } $cart = new Cart(); ?> Produc Page: <?php include '_class/core.php'; include '_class/cart.php'; $core->session($mode = 'start'); if(isset($_POST['buy'])) { $cart->add_product(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Product</title> </head> <body> <?php $core->navbar();?> <form method="post" action=""> <input name="product_id" type="hidden" value="Coffee" /> <h1>Cup Of Coffee</h1> <p>Buy me you know you want too!</p> <input name="buy" type="submit" value="Add To Basket" id="buy" /> </form> <form method="post" action=""> <input name="product_id" type="hidden" value="Biscuits" /> <h1>Barrel Of Biscuits</h1> <p>When Coffee Just Gives You The Munchies!</p> <input name="buy" type="submit" value="Add To Basket" id="buy" /> </form> </body> </html> my include cart file: <?php if (isset($_SESSION['cart'])) {//Session Cart foreach($_SESSION['cart'] as $product_id => $quantity){//Session Array if($quantity < 1){//Quantity Less Then 1 unset($_SESSION['cart'][$product_id]); echo "<script>window.location='cart.php'</script>"; }//End Quantity Less Then 1 if(!$_SESSION['cart']){//Cart Array Empty unset($_SESSION['cart']); echo "<script>window.location='cart.php'</script>"; }//End Cart Array Empty if ($product_id != NULL){//Show Products echo '<h1>' . $product_id . '</h1>'; echo "Quantity <br>"; ?><a href="cart.php?product_id=<?php echo $product_id; ?>&option=increase">+</a> <?php echo $quantity; ?> <a href="cart.php?product_id=<?php echo $product_id; ?>&option=decrease">-</a><?php }//End Show Products }//End Session Array require 'library/paypal.php'; }else{ echo "Your Basket Is Empty"; }//End Session Cart ?> and my actual cart file <?php include '_class/core.php'; include '_class/cart.php'; //$core->session($mode = 'start'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Cart</title> </head> <body> <?php $core->navbar(); if (isset($_GET['product_id']['option'])) { $product_id = $_GET['product_id']; $option = $_GET['option']; if ($option == 'decrease'){ $cart->decrease_product(); } if ($option == 'increase'){ $cart->increase_product(); } }elseif (isset($_GET['option'])){ $option = $_GET['option']; if ($option == 'empty'){ $cart->empty_cart(); } }else{ require 'library/cart.php'; } ?> <p><a href="cart.php?option=empty">Empty Cart</a></p> </body> </html> thanks again Quote Link to comment https://forums.phpfreaks.com/topic/258172-_sessioncart-help/#findComment-1323527 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.