Jump to content

Session problem


dc_jt

Recommended Posts

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?
			}

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.