User Name
Password
AppleNova Forums » Programmer's Nook »

Quick PHP Array question


Register Members List Calendar Search FAQ Posting Guidelines
Quick PHP Array question
Thread Tools
torifile
Less than Stellar Member
 
Join Date: May 2004
Location: Durham, NC
Send a message via AIM to torifile  
2009-09-26, 01:18

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
rollercoaster375
Senior Member
 
Join Date: Mar 2005
Location: UIllinois (Champaign, IL)
Send a message via AIM to rollercoaster375 Send a message via MSN to rollercoaster375 Send a message via Yahoo to rollercoaster375 Send a message via Skype™ to rollercoaster375 
2009-09-26, 12:01

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
Brad
Selfish Heathen
 
Join Date: May 2004
Location: Zone of Pain
 
2009-09-26, 14:12

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
ast3r3x
25 chars of wasted space.
 
Join Date: May 2004
Send a message via AIM to ast3r3x  
2009-09-27, 08:06

I'll put in a vote for Brad's style of making the cart and workshops into classes.
  quote
torifile
Less than Stellar Member
 
Join Date: May 2004
Location: Durham, NC
Send a message via AIM to torifile  
2009-10-02, 14:55

I'll come back to this thread soon. Thanks, guys. I knew I could count on you!
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
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


« Previous Thread | Next Thread »

All times are GMT -5. The time now is 05:26.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2024, AppleNova