Jump to content

a little tough problem here - need a variable


zang8027

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.