dc_jt Posted February 20, 2008 Share Posted February 20, 2008 Hi I am doing a shopping cart and Ive got a form which acts as one product. Therefore when you submit this form it adds product 9999 to the basket. If you were to submit it again it would add product 9999 again, instead this time the quantity would be 2. However, I need to retain what was submitted in this form so that when they go to the basket it says underneath product 9999 the info that were submitted for BOTH forms. I was thinking of saving the submitted info in a session, however if I use the same session variables the second one would just overwrite the first. therefore how can I use different session variables depending on how many times the form has been submitted. Can you increment a variable? Thanks if(!$_SESSION['shape']){ $_SESSION['shape'] = $_POST['shape']; } else { //increment variable here? and add $_POST['shape'] to $_SESSION['shape'] + 1? } Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 20, 2008 Share Posted February 20, 2008 Use an array. Not knowing your existing variables and code, the simplified version would be - $_SESSION['shape'] = array(); // initially create an array (only do this once when the cart is created.) $_SESSION['shape'][] = $_POST['shape']; // add each item, creating a new array element for each one // access all the items added foreach($_SESSION['shape'] as $value) { // $value contains each $_POST['shape'] that was added } Quote Link to comment Share on other sites More sharing options...
dc_jt Posted February 20, 2008 Author Share Posted February 20, 2008 Cheers, thanks for your help thats great. However now on my basket page I need to split all the $_SESSION['shape'] up so that if they have submitted a form with a round shape and then submitted a form with a heart shape, when it comes to the basket it will say: Product: 9999 Shape: Heart Product: 9999 Shape: Round Ideally, I would like to somehow link the session to the form, such as heart would be linked to form submission 1 and round to form submission 2, then when I come to print it out I could print all values for form 1 and then form 2? Hope that makes sense. Thanks again Quote Link to comment Share on other sites More sharing options...
dc_jt Posted February 20, 2008 Author Share Posted February 20, 2008 I have done the following: if(!$_SESSION['count_form_submissions']){ $_SESSION['count_form_submissions'] = array(); } $_SESSION['count_form_submissions'][] = count($_SESSION['count_form_submissions']) +1; And then in basket done this: foreach($_SESSION['count_form_submissions'] as $iCount) { echo "Cookie ".$iCount."<br />"; echo "Flavour: ". $_SESSION['flavour'][$iCount-1]."<br />"; echo "Shape: ". $_SESSION['shape'][$iCount-1]."<br />"; ?> <?php } }?> This doesnt seem to be the best method though. Anyone improve this? Thanks 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.