Less than Stellar Member
|
I'm working on a shopping cart thingy. Buyers can choose one of workshops A or B and C or D. I need to make sure that they don't choose A and B at the same time. While they're browsing, they can choose to add Workshop A to their cart and then continue to read about B.
If they've got workshop A in their cart, I'd like to be able to include a little warning box saying "You've already got a morning workshop selection. Please remove it before adding another" or something like that. The cart stores the items in a PHP array like so: [php] Array ( [0] => stdClass Object ( [cart_item_id] => 13 [cart_id] => 1 [nid] => 12 [qty] => 1 [changed] => 1253945472 [/php] I'd like to be able to parse that data and say if there's an [nid] of 9 (workshop A), they get that warning. Any ideas on how to do this? Sorry about the incoherence. It's late and my brain isn't working so well right now. If it's not red and showing substantial musculature, you're wearing it wrong. |
quote |
Senior Member
|
I'm assuming that's set to a variable of some sort... In which case, all you do is use something to effect of...
Code:
if ( $array['nid'] == 12 ) {
// do whatever
} |
quote |
Selfish Heathen
Join Date: May 2004
Location: Zone of Pain
|
Yup, what roller said.
Although, long-term I'd suggest using a couple of classes to represent your data rather than keyed arrays. That way you'll be able to encapsulate the data and have the code manage the data for you rather than touching it directly, which can protect you from potential bugs. Given what I've seen so far, I might spin up and use couple of classes like the following. You could have a Cart class that is effectively an intelligent container of an array of CartItem class objects: Code:
class Cart {
protected $cartId;
protected $cartItems;
public function hasItemWithNid ( $nid ) {
foreach ( $this->cartItems as $cartItem ) {
if ( $cartItem->getNid() == $nid ) {
return TRUE;
}
}
return FALSE;
}
public function getItemNotices ( ) {
$notices = array();
foreach ( $this->cartItems as $cartItem ) {
$notice = $cartItem->getNotice();
if ( $notice != NULL ) {
$notices[] = $notice;
}
}
return $notices;
}
public function addItemWithNidForQuantity ( $nid, $quantity ) { ... }
public function removeItemWithNidForQuantity ( $nid, $quantity ) { ... }
...
} Code:
class CartItem {
protected $cartItemId;
protected $nid;
protected $quantity;
public function getNid ( ) {
rerturn $this->nid;
}
public function getNotice ( ) {
if ( $this->nid == 9 ) {
return "You already have a morning workshop. Please remove, etc...";
} elseif ( $this->nid == 10 ) {
return "You already have an evening workshop. Please remove, etc...";
} elseif ( ... ) {
return ...
} else {
return NULL;
}
}
public function updateQuantity ( ) { ... }
public function getQuantity ( ) { ... }
...
} And in the top-level code page, you could have: Code:
...
$notices = $cart->getItemNotices();
foreach ( $notices as $notice ) {
print '<div style="notice">'.$notice.'</div>';
}
... ...but then again this is the kind of thing I do at my day job. The quality of this board depends on the quality of the posts. The only way to guarantee thoughtful, informative discussion is to write thoughtful, informative posts. AppleNova is not a real-time chat forum. You have time to compose messages and edit them before and after posting. |
quote |
25 chars of wasted space.
|
I'll put in a vote for Brad's style of making the cart and workshops into classes.
|
quote |
Less than Stellar Member
|
I'll come back to this thread soon. Thanks, guys. I knew I could count on you!
|
quote |
Posting Rules | Navigation |
|
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
1 quick mbp question | meepzork | Purchasing Advice | 5 | 2006-05-30 20:33 |
Quick Question | zman2005 | Genius Bar | 6 | 2006-05-10 23:00 |
Quick eMac question | torifile | General Discussion | 18 | 2004-10-20 09:02 |