ball420 Posted June 24, 2008 Share Posted June 24, 2008 here is my issue.... i have a small shopping cart meaning 20 items, i have it set up now that when you click on an item you go to the next page and the item info shows. Here is the code from the first page <form action="page2.php" method="post"> <input type="hidden" name="item_name" value="A-07"> <input type="hidden" name="amount" value="55.00"> <input type="hidden" name="no_shipping" value="2"> <input type="image" src="SM.gif" border="0" name="submit" > <img alt="" border="0" src="pixel.gif" width="1" height="1"> </form> what i want to do is to be able to go back to the product pages but still keep that item in the "session" so when you go to the checkout page all those items are there. I understand that i will need to use sessions but how do i differentiate between items if each item has the same variables within the <form></form>? I hope I'm explaining correctly. Thanks for the guidance it's much appreciated. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 24, 2008 Share Posted June 24, 2008 The way I'd do it is: firstly, start a session in every page, so that as they browse, if a session variable is created, it wont go away if they view a page that doesnt have a session(or changes the current session). I'd make a list of values of the items that were ordered separated by something. One way you can do it is when an item is add to the cart, you do $_SESSION['in_cart'] = $_SESSION['in_cart']."$item_id|"; Then when you're going to the "checkout", you'd explode the session variable separating the |'s like.. $var = explode("|", $_SESSION['in_cart']) so that the number of items is sizeof($var)-1 (since the last value will be blank, due to the right side of the last | being counted in the array). Quote Link to comment Share on other sites More sharing options...
ball420 Posted June 24, 2008 Author Share Posted June 24, 2008 so when i explode at the "checkout" the session will store all the input fields from each <form> </form> that's posted? thanks for the guidance much appricated Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 24, 2008 Share Posted June 24, 2008 Well, each time a form is submitted, you should truncate($_SESSION .= value) the items. So at checkout, you dont care about the forms, everything should have already been stored Quote Link to comment Share on other sites More sharing options...
ball420 Posted June 27, 2008 Author Share Posted June 27, 2008 this is what i have, $_SESSION['mycart']= $_POST['mycart'];."item_name|"; so that I understand the session named 'mycart' will be carried from page to page given that I carry this same session on each page using <?php session_start(); $_SESSION['mycart']= $_POST['mycart'];."item_name|"; session_write_close(); ?> then each product with the the form name="item_name" will line up when each product is submitted again with the same form name="item_name" but with different values. then at the end when i want to use all the vars collected in the session i use explode. thanks for your guidance I hope I didn't sound too confusing. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 27, 2008 Share Posted June 27, 2008 this is what i have, $_SESSION['mycart']= $_POST['mycart'];."item_name|"; so that I understand the session named 'mycart' will be carried from page to page given that I carry this same session on each page using <?php session_start(); $_SESSION['mycart']= $_POST['mycart'];."item_name|"; session_write_close(); ?> then each product with the the form name="item_name" will line up when each product is submitted again with the same form name="item_name" but with different values. then at the end when i want to use all the vars collected in the session i use explode. thanks for your guidance I hope I didn't sound too confusing. remove the semi-colon after $_POST['mycart'] and I'm not positive, but did you have another question? or were you just explaining that you understand whats going on? Quote Link to comment Share on other sites More sharing options...
ball420 Posted June 28, 2008 Author Share Posted June 28, 2008 thanks for your help i was just explaining what I thought was going on. So i guess i have it down I'm going to get it all set up I think i got it now. thanks again 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.