3raser Posted June 29, 2010 Share Posted June 29, 2010 How do I make like this: Someone clicks on item one or two, and it adds it to the cart. So say I added Item One to my cart two times, and Item Two to my cart three times. The session should (this is what I want it to do) look like this: 1-1-2-2-2 I'm trying to achieve this via the explode function. <?php $item_1 = "Item One <a href='loops_test.php?add=1'>Add Item</a>"; $item_2 = "Item Two <a href='loops_test.php?add=2'>Add Item</a>"; if($_GET['add'] == 1 || $_GET['add'] == 2) { $item = $_GET['add']; if($item == 1) { $item = 1; } else { $item = 2; } $generate_items = explode("-", $session); $_SESSION['items']=$generate_items; } echo $item_1."<br/>"; echo $item_2."<br/>"; echo $_SESSION['items']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/206168-adding-items-to-cart/ Share on other sites More sharing options...
Adam Posted June 29, 2010 Share Posted June 29, 2010 Why not just store them as an array? Quote Link to comment https://forums.phpfreaks.com/topic/206168-adding-items-to-cart/#findComment-1078654 Share on other sites More sharing options...
3raser Posted June 29, 2010 Author Share Posted June 29, 2010 Why not just store them as an array? What do you mean? I believe that is what I'm trying to do with the session. Quote Link to comment https://forums.phpfreaks.com/topic/206168-adding-items-to-cart/#findComment-1078656 Share on other sites More sharing options...
Adam Posted June 29, 2010 Share Posted June 29, 2010 Sorry, didn't look too much into your code after your comment: So say I added Item One to my cart two times, and Item Two to my cart three times. The session should (this is what I want it to do) look like this: 1-1-2-2-2 Your IF block is totally redundant. Why store $item as 1 or 2, when you know $_GET['add'] is equal to that already? Where is $session coming from? $item doesn't get stored at all in your code. -- I think you need to plan this out a little more. Where do these product IDs (1 & 2) come from? Quote Link to comment https://forums.phpfreaks.com/topic/206168-adding-items-to-cart/#findComment-1078709 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.