Re: Creating an OO Shopping Cart

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 05:14 PM 4/20/2006, Steve wrote:
I'm creating my own Object Oriented PHP Shopping Cart.

I'm confused about the best way to call the functions of the class given the "static" nature of the web.

For example, if I have a function addItem($code), how will this be called from the catalog or product description pages where the BUY buttons are?

On the product description page (say for product ABC213), there will be a BUY button, but when the button is clicked, obviously I cannot immediately call $cart->addItem('ABC213'). So how is this done?


Steve,

One way to preserve your cart object between page requests is to keep it serialized in the session.

When the PHP program wakes up:

        // begin session processing
        session_start();

                // if the cart already exists...
                if (isset($_SESSION["cart"])
                {
                        // read the cart from the session
                        $oCart = unserialize($_SESSION["cart"]);
                }else{
                        // or create it
                        $oCart = new CartObject();
                }

Before the PHP script ends:

        // save the cart to the session
        $_SESSION["cart"] = serialize($oCart);

In the body of the script, you'll use $_GET and/or $_POST arrays to feed user input into the cart object, and echo functions to write each new page in response.

Does this help clarify, or am I stating only what you already know?

Paul
_________________________

Refs:
http://php.net/session
http://php.net/session_start

http://php.net/serialize
http://php.net/unserialize

http://php.net/isset

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux