• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

PHP Multiple values in one session variable

Hello!

I am trying to make my own simple shopping cart by way of $_SESSION  vars.  I really dont know how to use the serialize or unserialize functions, but am willing to learn.

Basically i need to store both item number AND price in the variable, and possibly puting those variables in an array. But i dont know how to have a variable hold two or more values.

Old Cheap Way: product id and quantity
$_SESSSION[PRODUCT_6100] = "8";
$_SESSSION[PRODUCT_4100] = "6";
$_SESSSION[PRODUCT_2100] = "9";

But i want it to say
$_SESSION[PRODUCTS_IN_CART] = "6100 x 8, 4100 x 6, 2100 x 9";  Or something like that

I have heard that putting all like-minded things like "products ordered" in the same array is better than multiple _Session variables because you can do functions on them like array_unique and array_merge and stuff like that, though i don't know how i will use them yet.  

I do know that i want to be able to break them all out to change qty and delete individual ones in the shopping cart page, then send them to the payment gateway, but keep them together so that i can just add another product and quantity to the "products in cart" session variable.  
Thanks All!
-mxgrogg-
0
mxgrogg
Asked:
mxgrogg
  • 6
  • 4
  • 2
  • +1
1 Solution
 
jdpipeCommented:
I did this sort of thing recently for a quick shopping cart that works without a database... I'll get the link for you now...
JP
0
 
AlanJDMCommented:
I would stick with your old method but add a dimension to your array...

$_SESSSION['PRODUCTS_IN_CART']['PRODUCT_6100'] = "8";
$_SESSSION['PRODUCTS_IN_CART']['PRODUCT_4100'] = "6";
$_SESSSION['PRODUCTS_IN_CART']['PRODUCT_2100'] = "9";

This way you have the single session variable you wanted, and you don't have the parsing issues you whould inherit by combining multiple items into a single string.


Alan
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
minnirokCommented:
If you're already using session variables, theres no sense in concatenating into Coma Seperated Values when you don't have to.  You can use a multidimensional session variable.  I would recommend one of the form:

$_SESSION['cart'][item_id] = quantity;

so, you store all your items in a single session variable 'cart' rather than one named after each product item.  The session variable cart holds an array of all items within it.
each item within the cart, in turn holds the quantity value...

this makes it easier for you to process rather than having to parse back out all the data you just crunched into one variable (which you could do w/ the explode and implode array functions)

i find array serialization helpful for long term storage of array data but it wouldn't be of any benefit in this case unless you were wanting to store the cart purchases in a database or write them to file...
0
 
jdpipeCommented:
BTW that solution I gave there has all you data within the $_SESSION['order'] variable, as you can see here:

$order =& $_SESSION['order']

As minnirok mentions, there's no particular advantage in concatenating SESSION stuff. You should happily rely on lower-level processing for this stuff, don't do it all yourself, as it will be slower that way. Also, remember that $_SESSION variables aren't transferred to the client, so you don't need to worry about keeping them compact.

JP
0
 
AlanJDMCommented:
minnirok

I believe I had already stated the same thing you just did, no?


Alan
0
 
mxgroggAuthor Commented:
dang, you guys are amazing!  Alan was the first to say what you all ended up saying.  Thanks guys.
-mxgrogg-
0
 
minnirokCommented:
Alan you've become thoroughly paranoid of people stealing your points ;)

We essentailly made the same point.  You just posted a few seconds before mine :D
0
 
AlanJDMCommented:
Sorry minnirok,  I hadn't noticed there was only one minute between posts. Rest assured, there is no paranoia here, just have a pet peeve for plagerism. ;-)


Alan
0
 
jdpipeCommented:

one important aside, you should use quotes (pref single quotes) around your variable indices.
0
 
AlanJDMCommented:
Excellent point jdpipe, your absolutely correct, I should have caught that as well. Thanks for pointing that out!


Alan
0
 
AlanJDMCommented:
Please do, I would appreciate it.


Alan
0
 
AlanJDMCommented:
Yes, I know that, however for me that falls under "bad habbit that is hard to break". Please don't hesitate to beat me up any time you see me doing that.


Alan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now