magicmoose Posted March 12, 2007 Share Posted March 12, 2007 Hi, I'm trying to make a simple shopping basket script, but am a little unsure of the best way to do it. I want an array where items can be added/removed with a simple form and displayed as a list, but I'm a little unsure how to go about doing it. Here's what I have so far... On the page before this is a simple 'add to basket' form, which executes this. <? $add = $_POST['add']; if (isset ($add)) { echo $_SESSION['selectedcomic']; } else { echo "Your request could not be carried out"; } ?> The echo statement is just for me to check that the form was working. I need a SESSION array to hold each product that gets added. I thought you could have an empty array by just using [] and then adding to it later, but I haven't been able to get that to work yet. Any nudges in the right direction would be appreciated. Thanks. Quote Link to comment Share on other sites More sharing options...
papaface Posted March 12, 2007 Share Posted March 12, 2007 If you are using sessions you need session_start(); at the top of the php file: <?php session_start(); $add = $_POST['add']; if (isset ($add)) { echo $_SESSION['selectedcomic']; } else { echo "Your request could not be carried out"; } ?> Quote Link to comment Share on other sites More sharing options...
magicmoose Posted March 12, 2007 Author Share Posted March 12, 2007 Sorry, should have mentioned that. I have that at the top of the file, this is just a small section from it. Quote Link to comment Share on other sites More sharing options...
magicmoose Posted March 12, 2007 Author Share Posted March 12, 2007 OK, I have added some more, but I am still having trouble. I now have the following... <? $add = $_POST['add']; //variable say if a value was sent $selected=$_SESSION['selectedcomic']; //the item selected if (!isset($SESSION['basket'])) //if array dosent exist, create it { $_SESSION['basket'] = array(); array_push($_SESSION['basket'],$selected); //add chosen item to array } if (isset ($add)) { //if item was added, display contents of array echo (array_values($_SESSION['basket'])); } else { echo "Your request could not be carried out"; } ?> But all that gets displayed is the word "Array" Can anyone see where I'm going wrong? Thanks. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 13, 2007 Share Posted March 13, 2007 You can't just echo an array, you need the recursive print function print_r() Change echo (array_values($_SESSION['basket'])); to echo '<pre>', print_r($_SESSION['basket']), '</pre>'; Quote Link to comment Share on other sites More sharing options...
Barand Posted March 13, 2007 Share Posted March 13, 2007 Also if (!isset($SESSION['basket'])) //if array dosent exist, create it { $_SESSION['basket'] = array(); array_push($_SESSION['basket'],$selected); //add chosen item to array } You only add an an item if the array did not exist. Above should be if (!isset($SESSION['basket'])) //if array dosent exist, create it { $_SESSION['basket'] = array(); } array_push($_SESSION['basket'],$selected); //add chosen item to array or if (!isset($SESSION['basket'])) //if array dosent exist, create it { $_SESSION['basket'] = array(); } $_SESSION['basket'][] = $selected; //add chosen item to array Quote Link to comment Share on other sites More sharing options...
magicmoose Posted March 13, 2007 Author Share Posted March 13, 2007 OK, thanks. That seems to have helped, however it now displays... Array ( [0] => V for Vendetta ) 1 I just want the "V for Vendetta" part. Any idea where the other stuff is coming from? Quote Link to comment Share on other sites More sharing options...
Barand Posted March 13, 2007 Share Posted March 13, 2007 I thought you were just checking the array content. Change echo '<pre>', print_r($_SESSION['basket']), '</pre>'; to foreach ($_SESSION['basket'] as $item) { echo "$item<br />'; } Quote Link to comment Share on other sites More sharing options...
magicmoose Posted March 14, 2007 Author Share Posted March 14, 2007 Sorry I have not replied sooner, I lost my internet connection for a day:( Thanks for your help, this is almost working now, except that for some reason it only ever displays the last item added. I think rather than adding a new item to the array, it is just replacing the old one each time a new item is added. Any idea why? 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.