Jump to content

Recommended Posts

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
         )

)

Link to comment
https://forums.phpfreaks.com/topic/296837-_session-and-stored-info/
Share on other sites

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

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);
  • Like 1

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.