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
Share on other sites

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

Link to comment
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

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.