Current time: 04-20-2014, 04:43 AM Hello There, Guest! (LoginRegister)

Post Reply 
PHP multi-dimensional array issue
04-29-2006, 07:06 AM
Post: #1
PHP multi-dimensional array issue
Hi! I'm trying to add items to my shopping cart using PHP. I'm allowing item options that may modify the item, ie item num 123 may have options s, l, xl. If the user has an item in the cart that is "s" and add an "xl", the cart should add a new item rather than update the qty because the actual item is different.

Currently, I'm using multidimensional arrays in the Session to do this. But...it's not working. What I really need to do is an exact comparison on the option array of the item the user is trying to add to what is already in the cart. I looked in the PHP manual, considered using array_diff, but a) I don't know if it will work on the subarrays in the main options array b) the manual doesn't say what array_diff returns if the arrays are exactly alike.

The structure of the array is like this $_SESSION["item"][0]["options"]["Color"][0] = "12 if the subarray of options is an array or $_SESSION["item"][0]["options"]["Size"]= "1" if the options subarray only contains a value.

If there is an easier way to do this, please let me know. This is my first shopping cart app and also my first attempt at using multidimensional arrays, so maybe I'm making this a lot harder than it has to be.

$options is the array of the item the user is trying to add.

Here's the code:
foreach ($options as $opt_key => $opt_value) {
if(!array_key_exists($opt_key, $_SESSION["item"][$key]["options"])) {
$update_qty = "n";
}
else {
$sess_sub_value = $_SESSION["item"][$key]["options"][$opt_key];
//debug start
if(is_array($sess_sub_value)) { echo "<h1>Session Sub</h1>"; foreach($sess_sub_value as $sub_key => $sub_value) { echo $sub_key ." = ".$sub_value."
"; }}
else {
echo "<hr>
session sub value=".$sess_sub_value."
"; }
//debug end
// I think the problem is here. If it finds one value the same, it changes to update and it shouldn't. I think I should be using a diff here or something.
if(is_array($opt_value)) {
foreach($opt_value as $opt_sub_key => $opt_sub_value) {
if((in_array($opt_sub_value, $sess_sub_value)) || ($update_qty !=="n")){
$success["result"] = "n";
$success["loop2"] = "2causing issues";
$update_qty = $key;
// change to yes
echo "
Loop 2 hits here
";
}
else {
$update_qty="n";
continue 3;
}
}
}
else {
// put stuff here
if(($opt_value !== $sess_sub_value)||($update_qty =="n")){
$update_qty ="n";
// change to yes
$success["result"] = "n";
$success["loop4"] = "4causing issues";
}
else {
$update_qty=$key;
continue 2;
}
}
}

}

================================
CrimsonDryad Web Design Services
Web Design, Custom Software Development
http://www.crimsondryad.com
Visit this user's website Find all posts by this user
Quote this message in a reply
05-05-2006, 05:04 AM
Post: #2
PHP multi-dimensional array issue
Quote:Hi! I'm trying to add items to my shopping cart using PHP. I'm allowing item options that may modify the item, ie item num 123 may have options s, l, xl. If the user has an item in the cart that is "s" and add an "xl", the cart should add a new item rather than update the qty because the actual item is different.
Currently, I'm using multidimensional arrays in the Session to do this.
You're braver than me; I use single-dimension arrays for this, and I definitely don't use them to track color and size within the shopping cart itself; the cart itself only tracks the SKU (stock keeping unit) and the quantity ordered.

I use two separate databases to track product info and stock info; the product information is general, the stock (SKU) info contains a reference back to the parent product with each combination of color and size having it's own SKU. This arrangement allows not only for an easier shopping cart, it also allows the client to keep track of inventory by general product and then drill down to what's left for each color/size within that product.

Quote:But...it's not working. What I really need to do is an exact comparison on the option array of the item the user is trying to add to what is already in the cart. I looked in the PHP manual, considered using array_diff, but a) I don't know if it will work on the subarrays in the main options array b) the manual doesn't say what array_diff returns if the arrays are exactly alike.
It returns a null for identical arrays.

I do mine even uglier (but much simpler); I have two 1D arrays, one for the SKU and one for the quantity ordered (I didn't use an SKU-keyed array for some reason, but it's been long enough for me to forget why). Anyways, when shopper adds a quantity of an item (or items) I simply run a quick loop though the SKU array for each item and add the new item if necessary or increment the quantity if the SKU is already present in the cart.

I can't speak to how array_diff works with multi-dimmed arrays, but it sounds...a little too crunchy for me.
Find all posts by this user
Quote this message in a reply
05-05-2006, 06:13 AM
Post: #3
PHP multi-dimensional array issue
Thanks for replying, so far you've been the only one brave enough to. Smile

As for me, I'm not so sure if it's bravery so much as stupidity. This is my first shopping cart app, and I'm learning a LOT.

Ok, so suppose your client has multiple sizes of say, T-shirts. How do you make that one item on the front end with a dropdown to select size? Is there a master sku you're using?

My business partner was really impressed because he says no one else offers some of the features we have. I'm quickly discovering why no one else offers those features. Oi.

thanks again....

================================
CrimsonDryad Web Design Services
Web Design, Custom Software Development
http://www.crimsondryad.com
Visit this user's website Find all posts by this user
Quote this message in a reply
05-05-2006, 10:14 AM
Post: #4
PHP multi-dimensional array issue
Quote:Thanks for replying, so far you've been the only one brave enough to. Smile
For all values of my saying "I wouldn't do it that way" equalling "brave", I suppose. Smile

Quote:As for me, I'm not so sure if it's bravery so much as stupidity. This is my first shopping cart app, and I'm learning a LOT.
Mine's about 3 years old; it's simple and it actually works, and now one client wants to implement coupons/discount codes. I'm still trying to figure out how to deal with that on an order that mixes taxable and non-taxable items.

Quote:Ok, so suppose your client has multiple sizes of say, T-shirts. How do you make that one item on the front end with a dropdown to select size?
I list each size individually; although drop-downs definitely work better for clothing, the client also sells fishing gear of different weights, etc. where a customer might want to order several different sizes. I suppose I coulda made a flag to display one way or the other, but too much hassle at the time.

Quote:Is there a master sku you're using?
Yes and no. Yes, there's a master SKU, but I call it a "Product" record. The product record basically defines:
1) Brand (obvious)
2) Model Number (obvious)
3) Base price
4) Scale code (relates to a separate table with available sizes)
5) Pricing Scalability (yes/no)
6) Taxablity (yes/no)
7) Stocked or non-stocked (for inventory control only)
8) Features, description, MSRP, pictures, etc.
..with the primary key being Brand Name + Model Number

The actual Stock record (SKU)
1) Brand Name + Model Number (to point back to parent)
2) Color
3) Size
4) Price (which usually = product base price, but can be adjusted for smaller or larger weights/sizes)
5) Quantity-on-hand (which is ignored for product records that are "non-stocked")
6) Item Weight (I have to pay attention to this for shipping purposes)
...with the primary key being Brand name + model Number + color code + scale code (from product record) + size. That's what gets put into the first shopping cart array.

Basically, each Stock record is an individual color/size combo that points back to the parent record where it gets the rest of its info.

Quote:My business partner was really impressed because he says no one else offers some of the features we have. I'm quickly discovering why no one else offers those features. Oi.
Shopping carts are scary, but not nearly as scary as a series of input forms designed to calculate/compare costs of disposing of hospital-grade liquid medical waste. Icky stuff, that.

But if the shopping cart thing gets to really kicking your butt too much and ya wanna try something more database-driven (as opposed to array-tracked), send along an email (the address in my profile goes straight to me) and I'll provide ya some more details (SQL table templates, code snippets) to help ya along - it'll definitely get way too complicated to post back-n-forth in this forum.
Find all posts by this user
Quote this message in a reply
05-15-2006, 03:47 PM
Post: #5
PHP multi-dimensional array issue
I'm sure you know that print_r() will show the contents of your multidimensional arrays for debugging purposes.

If your structure is really complex, you might try using objects instead of multidimensional arrays, although I've not done this.

I might also mention that a good debugger might help.
Zend is the best and most stable and you can try it free for 30 days. Activestate Komodo might work well enough and is only $30. These would show the contents of your arrays as the program runs and might allow you to fix the problems.
Find all posts by this user
Quote this message in a reply
05-16-2006, 03:07 PM
Post: #6
PHP multi-dimensional array issue
If you plan to be complex, I'd highly recommend using objects and classes. It can save a lot of re-development time down the road.

And instead of print_r, var_dump lists types and sizes with structure for an array.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: