Jump to content

using an array in session variables


Giddy Rob

Recommended Posts

Hi,

 

I am creating a shopping cart for the first time and I would like to store information for each item added to the shopping cart in a session variable. I have got as far as creating a multidimensional array to store the information that needs sending to the checkout for the item, but I am a little stuck on how to add to this array when another item is added to the cart. The code I am using to store the array is as follows:

 

$items=array(array($displays[0],$displays[1],$displays[2],$displays[3],$displays[4]));

 

How do I add another item to the items array? I know how to do it all in one go but the customers will go to different pages so it needs to get the array from the session variable and add another item. The whole thing will be stored as a session variable again.

 

Also, as this is the first time I have done this I am kinda making it up as I go along so is it possible for me to store lets say five items in the items array in a session variable then pull out all the information from that session variable when i need it?

 

Main question is am i trying something that is possible? if not what is the best way to execute what I am trying to do?

 

Any help would be great

 

Cheers in advance.

 

Rob

Link to comment
Share on other sites

right, i have changed my code to:

 

$_SESSION['items']=array(array($displays[0],$displays[1],$displays[2],$displays[3],$displays[4]));
echo $_SESSION['animals'][0][1];

 

I just need to know how to add another item into that array when the user decides to add a new item.

 

Cheers

Link to comment
Share on other sites

To add an item to an array:

 

1-dimensional:

<?php
$array = array('first', 'second', 'third');
// add a "fourth" value to the end of the array
$array[] = 'fourth';
// $array is now array('first', 'second', 'third', 'fourth')
?>

 

2-dimensional:

<?php
$array = array(array('first', 'second', 'third'), array('a', 'b', 'c'));
// add a "fourth" value to the end of the first second-level array
$array[0][] = 'fourth';
// and to the second
$array[1][] = 'd';
// $array is now array(array('first', 'second', 'third', 'fourth'), array('a', 'b', 'c', 'd'))
?>

 

Hope that helps!

Link to comment
Share on other sites

thanks for the help but I need to have for example:

 

$_SESSION['items']=array(array(1,2,3,4,5));

 

$_SESSION['items']=array(array(6,7,8,9,10));

 

So that if I do:

 

echo $_SESSION['items'][0][0];

 

The output will be 1, and if I do:

 

echo $_SESSION['items'][1][0];

 

The output will be 6

 

i know this doesnt work but I am not sure how to do it.

 

Hope that makes a bit more sense :)

Link to comment
Share on other sites

cheers,

 

figured it out from your code

 

$_SESSION['items'][] = array($displays[0],$displays[1],$displays[2],$displays[3],$displays[4]);

 

slaps a new item in the array

 

Rock and roll!

 

Thanks for the prod in the right direction :)

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.