Supervan Posted June 15, 2015 Share Posted June 15, 2015 Hi,I’m using PHP OOP and created an instance of the class and stored the content inside a $_SESSION['cart'] 1... I can validate, if “prid” exists in the session with this …. How would i check for "prid" and "prflavour" Mango ? if (isset($_SESSION['cart'][12])) 2... I need to update the quantity when both conditions "prid" and "prflavour" are met. how do i change this $_SESSION['cart'][$_GET['prid']]-> to work with prid and prflavour $_SESSION['cart'][$_GET['prid']]->updateQuantity(123) sample of the $_SESSION['cart'] Array( [13] => Item Object ( [prid:protected] => 13 [prname:protected] => Meal [prflavour:protected] => Mango [prqty:protected] => 10 ) [12] => Item Object ( [prid:protected] => 12 [prname:protected] => Milk [prflavour:protected] => [prqty:protected] => 123 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/296837-_session-and-stored-info/ Share on other sites More sharing options...
Muddy_Funster Posted June 15, 2015 Share Posted June 15, 2015 you can't do that with your current setup. It really doesn't look like you have a proper persistance model here. have a look at the following http://www.devshed.com/c/a/php/building-persistent-objects-in-php-5/ it should help you a bit with what you are trying to accomplish Quote Link to comment https://forums.phpfreaks.com/topic/296837-_session-and-stored-info/#findComment-1514000 Share on other sites More sharing options...
requinix Posted June 15, 2015 Share Posted June 15, 2015 prid and prflavour (and the other two) are protected, which means you can't change them in your code like you're trying to do. You need to use methods in the class. For example, 1. Use a getFlavour() method to get the flavor and check that it is "Mango" 2. Use updateQuantity() just like how you have there If your problem is finding out which object in the array is the Mango item then you need a loop like foreach ($_SESSION['cart'] as $prid => $item) { if ($item->getFlavour() == "Mango") { $item->updateQuantity(123); break; // don't need to check the rest of the cart } }What would be nicer than this is if you had a sort of ItemCollection that could do much of this work for you. One class representing an entire cart. Then that class could handle the searching part, and you could call it with code like $_SESSION['cart']->updateFlavourQuantity("Mango", 123); 1 Quote Link to comment https://forums.phpfreaks.com/topic/296837-_session-and-stored-info/#findComment-1514001 Share on other sites More sharing options...
mac_gyver Posted June 15, 2015 Share Posted June 15, 2015 different flavors, colors, or sizes of something are different things, each with a unique id. Quote Link to comment https://forums.phpfreaks.com/topic/296837-_session-and-stored-info/#findComment-1514002 Share on other sites More sharing options...
Supervan Posted June 15, 2015 Author Share Posted June 15, 2015 prid and prflavour (and the other two) are protected, which means you can't change them in your code like you're trying to do. You need to use methods in the class. For example, 1. Use a getFlavour() method to get the flavor and check that it is "Mango" 2. Use updateQuantity() just like how you have there If your problem is finding out which object in the array is the Mango item then you need a loop like foreach ($_SESSION['cart'] as $prid => $item) { if ($item->getFlavour() == "Mango") { $item->updateQuantity(123); break; // don't need to check the rest of the cart } }What would be nicer than this is if you had a sort of ItemCollection that could do much of this work for you. One class representing an entire cart. Then that class could handle the searching part, and you could call it with code like $_SESSION['cart']->updateFlavourQuantity("Mango", 123); Thank you... will try... Quote Link to comment https://forums.phpfreaks.com/topic/296837-_session-and-stored-info/#findComment-1514004 Share on other sites More sharing options...
Supervan Posted June 15, 2015 Author Share Posted June 15, 2015 Thank you all for responding Quote Link to comment https://forums.phpfreaks.com/topic/296837-_session-and-stored-info/#findComment-1514005 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.