[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP Multiple values in one session variable

Posted on 2005-05-13
15
Medium Priority
?
489 Views
Last Modified: 2008-02-01
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
Comment
Question by:mxgrogg
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 7

Expert Comment

by:jdpipe
ID: 13997066
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
 
LVL 9

Accepted Solution

by:
AlanJDM earned 2000 total points
ID: 13997083
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
 
LVL 7

Expert Comment

by:jdpipe
ID: 13997088
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Expert Comment

by:minnirok
ID: 13997092
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
 
LVL 7

Expert Comment

by:jdpipe
ID: 13997158
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
 
LVL 9

Expert Comment

by:AlanJDM
ID: 13997194
minnirok

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


Alan
0
 

Author Comment

by:mxgrogg
ID: 13997218
dang, you guys are amazing!  Alan was the first to say what you all ended up saying.  Thanks guys.
-mxgrogg-
0
 
LVL 7

Expert Comment

by:minnirok
ID: 13997256
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
 
LVL 9

Expert Comment

by:AlanJDM
ID: 13997343
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
 
LVL 7

Expert Comment

by:jdpipe
ID: 13997411

one important aside, you should use quotes (pref single quotes) around your variable indices.
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 13997485
Excellent point jdpipe, your absolutely correct, I should have caught that as well. Thanks for pointing that out!


Alan
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 13998250
Please do, I would appreciate it.


Alan
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 13998432
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question