Jump to content
Larry Ullman's Book Forums

A $_Session Multidimensional Array


Recommended Posts

Hi all,

I've been trying to do something for a few days and I can't get it to work.

 

The background is as follows. I have a form which users can obtain a quote for making curtains. The form and the quote itself work fine. Once they have obtained a quote they can add it to their basket. I have created a session that contains all the relevant quote information that can then be taken to a basket page. I can't store this information in a DB because, at this stage, the user has not registered or logged in. I used print_r to see that the session works OK. A single quote has been added to the session. So far standard interent shopping stuff.

 

However I need to write something that takes into account that the user may 'add to basket' and then go back a get a second curtain quote. Now, if that happens it will just overwrite the first quote and the user will still have only a single quote in the basket. So i thought about a multidimensional array for $_SESSION. So I created the following code:

$_SESSION[] = array(
 'fabric_drop' => $fabric_drop,
 'track_pole' => $track_pole,
 'track_pole_width' => $track_pole_width,
 'heading_tape' => $heading_tape,
 'lining' => $lining,
 'fullness' => $fullness,
 'interlined' => $interlined,
 'fabric_width' => $fabric_width,
 'final_curtain_quote' => $final_curtain_quote);

 

If I run the script with one set of numbers it gives me a quote and adds it to the session perfectly. If I then run it again it overwrites the first quote. How do I get it to create a second array with the second quote in it? I checked Larry's chapter on e-commerce but that deals with a DB of fixed products. These curtain quotes can, and generally are, completely different.

 

I did think about creating some sort of quote id that increments with each quote obtained. But how do I add it to the session array so that it shows: quote 1 = blah blah, quote 2 = blah, blh etc.?

 

I feel that the answer is an easy one and reflects on my wooly knowledge of multidimensional arrays.

 

Thanks

Paul

Link to comment
Share on other sites

You need to assign that array to a associative part of the sessions array. Something like this:

 

$_SESSON['basket'][] = array(
  'fabric_drop' => $fabric_drop,
  'track_pole' => $track_pole,
  'track_pole_width' => $track_pole_width,
  'heading_tape' => $heading_tape,
  'lining' => $lining,
  'fullness' => $fullness,
  'interlined' => $interlined,
  'fabric_width' => $fabric_width,
  'final_curtain_quote' => $final_curtain_quote
);

  • Upvote 1
Link to comment
Share on other sites

Antonio,

Thanks for your answer it worked well. A further question if I may? Can I assign an existing session array to a multidimensional session array with the following syntax for example:

$_SESSION['curtain_basket'][] = array (
'fabric_drop' => $_SESSION['curtain_quote']['fabric_drop']
);

 

Is the above correct?

 

Cheers

Paul

Link to comment
Share on other sites

Hi all,

Further to the above post I continued on and populated the rest of the fields (track_pole etc.) and using print_r could see that they were added to the $_SESSION['curtain_basket'] array OK. I added 3 different quotes and it shows up as keys 0,1 and 2 as expected. So that was great. Thanks for your help and I think I now understand multidimensional arrays a bit better.

 

Next I wanted to display all the quotes on a basket page. So I tried the following code:

foreach ($_SESSION['curtain_basket'] as $key => $list)
{
echo $key;
echo $list['fabric_drop'];
echo $list['track_pole'];
echo $list['track_pole_width'];
echo $list['heading_tape'];
echo $list['lining'];
echo $list['fullness'];
echo $list['interlined'];
echo $list['fabric_width'];
echo $list['final_curtain_quote'];
}

 

It came up the following message:

 

Notice: Undefined variable: _SESSION in C:\xampp\htdocs\Dis Soft Furnishings\web\basket.php on line 3

 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Dis Soft Furnishings\web\basket.php on line 3

 

I thought that it would display the $key as 0, then list all the values such as fabric_drop etc. then $key as 1 and list all the values and so on.

 

What am I missing? Can a foreach work with a multidimensional array?

 

Cheers

Paul

Link to comment
Share on other sites

My point is that, while I'm not 100% sure I understand the structure you're going for, I think you're incorrectly referencing your array.

For one, in your original post, you were talking about creating a multidimensional array, but according to your code in the original post, you haven't even created a multidimensional array, you've created a one-dimensional associative array.

As such, I think you're either misunderstanding the terminology or making a syntax error.

 

For example, give the following code, which you provide a couple of posts ago, for that to work, you need to have several arrays stored under the $_SESSION['curtain_basket'] element:

 

foreach ($_SESSION['curtain_basket'] as $key => $list)

{

echo $key;

echo $list['fabric_drop'];

echo $list['track_pole'];

echo $list['track_pole_width'];

echo $list['heading_tape'];

echo $list['lining'];

echo $list['fullness'];

echo $list['interlined'];

echo $list['fabric_width'];

echo $list['final_curtain_quote'];

}

 

What I'm asking is, is your array structure like the following?

 

$_SESSION['curtain_basket'][0] = array('fabric_drop' => value1, 'track_pole' => value2,..., 'final_curtain_quote' => value8);
$_SESSION['curtain_basket'][1] = array('fabric_drop' => value1, 'track_pole' => value2,..., 'final_curtain_quote' => value8);
$_SESSION['curtain_basket'][2] = array('fabric_drop' => value1, 'track_pole' => value2,..., 'final_curtain_quote' => value8);
$_SESSION['curtain_basket'][3] = array('fabric_drop' => value1, 'track_pole' => value2,..., 'final_curtain_quote' => value8);
$_SESSION['curtain_basket'][4] = array('fabric_drop' => value1, 'track_pole' => value2,..., 'final_curtain_quote' => value8);
$_SESSION['curtain_basket'][5] = array('fabric_drop' => value1, 'track_pole' => value2,..., 'final_curtain_quote' => value8);
$_SESSION['curtain_basket'][6] = array('fabric_drop' => value1, 'track_pole' => value2,..., 'final_curtain_quote' => value8);
...

 

Because if it's not, then your syntax is incorrect. I'm wondering if just $_SESSION instead of $_SESSION['curtain_basket'] would work, but since I don't know the actual structure, I can't say for sure.

Also, sorry in advance if I came off as terse or rude. Not my intention.

Link to comment
Share on other sites

HatleySan,

Fantastic.

 

I added the reference to each echo as above and it worked perfectly. You guessed my intention which is that each new array added to the basket is a seperate quote involving all those 9 parameters.

 

You most certainly didn't come accross as terse or rude.....I struggle with arrays and the foreach function. I'm glad it works but I'm still not at all sure why it works!

 

If I could break it down: After the first iteration of the foreach does $list contain all 9 fields which it then proceeds to list out as per the echo lines or just 'fabric_drop'?

 

Cheers

Paul

Link to comment
Share on other sites

Well, I had a feeling you were misunderstanding how arrays work, which is why I made that simple example in my first post. I was kinda hoping you'd play with my sample script and realize what you were doing wrong, but I suppose I was too brief and abstract to get the point across well. Sorry.

 

Anyway, regardless of what kind of index you are using for an array, when you declare a foreach loop like the following, $value will sequentially go through each value in the array:

 

foreach ($arr as $value) {

 echo $value;

}

 

And in the case of a multidimensional array, you could place a foreach loop within a foreach loop. For example:

 

foreach ($arr as $value) {

 foreach ($value as $innerValue) {

echo $innerValue;

 }

}

 

Anyway, I could "explain" how arrays work in PHP until the cows come home, but as we all know, while annoying and painful at times, the real learning comes from you sitting down by yourself and playing with them. I suggest using the PHP.net page I linked above, and then writing a simple script like I wrote above, and slowing adding onto the script, ensuring that you understand each step along the way.

 

Well, good luck.

Link to comment
Share on other sites

 Share

×
×
  • Create New...