gerkintrigg Posted August 10, 2009 Share Posted August 10, 2009 I have been making shopping carts for years, and previously used the same script on http://www.tingifts.co.uk/, which works fine. but for some reason on http://jewellerybasket.com/, the shopping cart just doesn't add anything to the variable. and I don't know why. The shopping cart script is here: <?php if (! isset($_SESSION['cart'])){ $_SESSION['cart']=array(); } if(!empty($_REQUEST['prod'])){ $new=$_REQUEST['prod']; if(!empty($_POST['a'])){ if ($_POST['a']=='a'){ foreach ($_SESSION['cart'] as $my_new => $qty){ if (($_POST[$my_new]=='')||($_POST[$my_new]=='0')){ unset($_SESSION['cart'][$my_new]);} else{ $_SESSION['cart'][$my_new]=$_POST[$my_new]; } } } else{ if (isset($_REQUEST['prod'])){ if(isset($_SESSION['cart'][$new])){ $_SESSION['cart'][$new]=$_SESSION['cart'][$new]+1; } else{ $_SESSION['cart'][$new] = 1; } } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169582-solved-shopping-carts-differences-in-the-same-script/ Share on other sites More sharing options...
ignace Posted August 10, 2009 Share Posted August 10, 2009 the shopping cart just doesn't add anything to the variable. It would be nice to know to which variable you are referring, plus have you started sessions? If not then $_SESSION is empty on each new request, which would explain why nothing is added. Quote Link to comment https://forums.phpfreaks.com/topic/169582-solved-shopping-carts-differences-in-the-same-script/#findComment-894777 Share on other sites More sharing options...
gerkintrigg Posted August 11, 2009 Author Share Posted August 11, 2009 I defined sessions on every page. The site that isn't working is almost an exact copy of the site that is working... The only differences are a slight change to the navigation and a search facility added. Sorry, the cart items should be coming directly from the $_SESSION['cart'] variable (an array). It would appear to be empty. Quote Link to comment https://forums.phpfreaks.com/topic/169582-solved-shopping-carts-differences-in-the-same-script/#findComment-895456 Share on other sites More sharing options...
gerkintrigg Posted August 11, 2009 Author Share Posted August 11, 2009 I confess, I have no idea what happened to that script... but it's working again now, after much playing. Here's the completed version... though it's not as elegant as I'd like. <?php # if there is no cart session open, then open one: if (! isset($_SESSION['cart'])){ $_SESSION['cart']=array(); } # if we are adding a new product to the cart: if(!empty($_REQUEST['prod'])){ #set the new product $new=$_REQUEST['prod']; # check to see if the security field is correct: if(!empty($_POST['a'])){ # security check to see if the quantity needs updating: if ($_POST['a']=='a'){ #run through the cart and check the quantities: foreach ($_SESSION['cart'] as $my_new => $qty){ #if the product quantity is empty or set to zero: if (($_POST[$my_new]=='')||($_POST[$my_new]=='0')){ # delete the product from the cart: unset($_SESSION['cart'][$my_new]); } #otherwise else{ # update the quantity $_SESSION['cart'][$my_new]=$_POST[$my_new]; } } } else{ if (isset($_REQUEST['prod'])){ if(isset($_SESSION['cart'][$new])){ $_SESSION['cart'][$new]=$_SESSION['cart'][$new]+1; } else{ $_SESSION['cart'][$new] = 1; } } } } else{ $_SESSION['cart'][$new] = 1; } } foreach ($_POST as $key => $value){ if ($key!='a'){ if (!empty($key) && (($value!='0')&&($value!=''))){ $_SESSION['cart'][$key]=$value; } else{ unset($_SESSION['cart'][$key]); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169582-solved-shopping-carts-differences-in-the-same-script/#findComment-895470 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.