Jump to content

Adding data to an array


magicmoose

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/42418-adding-data-to-an-array/
Share on other sites

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.

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

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.