zang8027 Posted February 2, 2009 Share Posted February 2, 2009 Ok, i have a shopping cart. I want to make it so that when you add an item from a restaurant.. say the restID = 8, I want to set a variable to 8 that exists as long as my shopping cart. This way, no one can add more than 1 restaurant's item to the same cart. Im using a lot of OOP for this cart so this may be hard for you guys to help me out here but here is some logic of it: class ShoppingCart{ private $items = array(); //*********FUNCTION ADDITEM********* public function AddItem($product_id) { if(array_key_exists($product_id, $this->items)) $this->items[$product_id] = $this->items[$product_id] + 1; else $this->items[$product_id] = 1; //SET STATIC VARIABLE THAT EXISTS AS LONG AS CART FOR REST ID. //THIS PREVENTS MORE THAN 1 REST (shipping,tax wont change) } } } That function is called when the item is added. SO I want to make a variable that exists through out the rest of the project. I dont want to declare it in there though because that is called everytime something is added and I dont want the user to be able to go back and add another one from a diff store and over write it. Here is my other code: //********************function set shopping cart******************** function set_shopping_cart($cart) { $_SESSION['cart'] = serialize($cart); } <?php require_once '../functions/Functions.php'; $shopping_cart = get_shopping_cart(); //Grab value being passed $product_id = $_REQUEST['id']; $restID = $_REQUEST['restId']; if(product_exists($product_id)) { $shopping_cart->AddItem($product_id); } set_shopping_cart($shopping_cart); ?> few functions in there that are not important to this problem. I guess i should first figure out how to make a variable that will stay the same until the user clicks out. Is it a constant or a static variable? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 2, 2009 Share Posted February 2, 2009 If I am understanding you correctly, the user may add different products from multiple restaurants - BUT you do not want them to add the same product from different restaurants. How about something like this: class ShoppingCart { private $items = array(); //*********FUNCTION ADDITEM********* public function AddItem($product_id, $rest_id) { if(array_key_exists($product_id, $this->items) && $this->rest_id[$product_id] == $rest_id) { $this->items[$product_id] = $this->items[$product_id] + 1; } else { $this->items[$product_id] = 1; $this->rest_id[$product_id] = $rest_id; } } } Although I can think of several ways to handle this based upon how you need the functionality to work. Quote Link to comment Share on other sites More sharing options...
zang8027 Posted February 2, 2009 Author Share Posted February 2, 2009 they CAN add other products from diff restaurants but I dont want them to Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.