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
https://forums.phpfreaks.com/topic/106919-using-an-array-in-session-variables/
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

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!

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 :)

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.