SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 26
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Comments on cart class

    Hi.

    I need to create a simple shopping system for a site.
    After some looks at what allready exists i found out they are all to advanced, so i decided to do my own simple cart class and order class.

    I am currently working on the cart class, and have a working version.

    The code:

    PHP Code:
    /**
    cart.class.php

    A simple shopping cart with php/mysql.

    Needed mysql table for application
    $sql = "CREATE TABLE IF NOT EXISTS Cart
    (
            ID INT NOT NULL AUTO_INCREMENT,
            UserID INT NOT NULL,
            ItemID INT NOT NULL,
            Quantity INT NOT NULL,
            Die INT NOT NULL,
            UNIQUE (
                    ID
            )
    )";

    Requires another table/file for items.
    Cart entries are based on a user id(int).

    Uses Eclipse DB library for db handling.
    See eclipse documentation for rewriting for other database types
    */



    class Cart {
            
    // private var $db.
            
    var $db;

            
    // private var $items
            
    var $items = array();
            
            
    // private var $username
            
    var $username;
            
            
    // private var $userid
            
    var $userid;
            
            
    // constructor
            
    function Cart($username=null) {
                    
    $this->db =& new MyDatabase('database''localhost');
                    
    $this->db->connect('root''password');
                    if (isset(
    $username)) {
                            
    $this->createCart($username);
                    }
            }
            
            
    // Function that returns a user id on username input
            
    function getUserID($username) {
                    
    $sql "SELECT ID FROM users WHERE Username LIKE '{$username}'";
                    
    $result $this->db->query($sql);
                    if (
    $result->getRowCount() == 0) {
                            return 
    $username;
                    }
                    else {
                            
    $userid $result->getRow(0ECLIPSE_DB_ASSOC);
                            return 
    $userid['ID'];
                    }
            }

            
    // Create cart function. Simply sets userid and username (likely to be modified)
            
    function createCart($username) {
                    
    $this->username $username;
                    
    $this->userid $this->getUserID($username);
            }
            
            
    // Function that adds an item to the items array.
            
    function addItemToCart($itemid$quantity) {
                    if (isset(
    $itemid) && isset($quantity) && is_numeric($itemid) && is_numeric($quantity)) {
                            if (
    array_key_exists($itemid$this->items)) {
                                    
    $this->items[$itemid] = $this->items[$itemid] + $quantity;
                            }
                            else {
                                    
    $this->items[$itemid] = $quantity;
                            }
                    }
            }
            
            
    // Deletes an item from the items array
            
    function deleteItemFromCart($itemid$quantity) {
                    if (isset(
    $itemid) && isset($quantity) && is_numeric($itemid) && is_numeric($quantity)) {
                            if (
    $this->items[$itemid] <= $quantity) {
                                    unset(
    $this->items[$itemid]);
                            }
                            else {
                                    
    $this->items[$itemid] = $this->items[$itemid] - $quantity;
                            }
                    }
            }
            
            
    // Empties all cart entries for the given user
            
    function emptyCart() {
                    
    $sql "DELETE FROM Cart WHERE UserID LIKE '{$this->userid}'";
                    
    $this->db->query($sql);
            }
            
            
    // Saves the cart entries to database
            
    function saveCart() {
                    if (!empty(
    $this->items)) {
                            
    $die time() + 14400;
                            foreach (
    $this->items as $itemid => $quantity) {
                                    
    $sql "INSERT INTO Cart (ID, UserID, ItemID, Quantity, Die)
                                            VALUES ('', '
    {$this->userid}', '$itemid', '$quantity', '$die')";
                                    
    $this->db->query($sql);
                            }
                    }
            }
            
            
    // Opens a cart for a given username
            
    function getCart($username) {
                    
    $sql "SELECT ItemID, Quantity FROM Cart WHERE UserID LIKE '{$this->userid}'";
                    
    $result $this->db->query($sql);
                    for (
    $it =& new QueryIterator($result); $it->isValid(); $it->next()) {
                            
    $entry =& $it->getCurrent();
                            
    $this->addItemToCart($entry['ItemID'], $entry['Quantity']);
                    }
                    return 
    $this->getContent($this->userid);
            }

            
    // Returns the content of the items array
            
    function getContent($username) {
                    return 
    $this->items;
            }
            
            
    // Returns an order object based
            // Subject for huge modifications
            
    function createOrder() {
                    
    $order =& new Order($this->username);
                    return 
    $order;
            }

    Usage could be like:

    PHP Code:
    include("cart.class.php");


    $cart =& new Cart('Findus');
    // addItemToCart($itemID, $quantity)
    $cart->addItemToCart(10021);
    $cart->addItemToCart(10031);
    $cart->addItemToCart(10044);
    $cart->addItemToCart(10022);

    // deleteItemFromCart($itemID, $quantity)
    $cart->deleteItemFromCart(10023);

    // Saves cart to database
    $cart->saveCart(); 
    What im looking for is general comments, things to improve.
    And i have some specific questions.

    Right now i save the cart entries to a database for temporary storage. And run a clean up script that deletes old stuff.
    Its either based on a userID or the sessionID. (I didnt think of non registered users when i started)
    How would you store the data between pages?
    I could pass the cart object in session vars.
    I could create temporary tables and pass the db connection in session vars (my inital thought).
    I could get the carts contet and pass it as a session var.

    As it is now i dont have to pass it, i can just load up the cart from db.

    Im not satisfied with that method, but i find none of the methods to be very elegant. Im leaning to passing the cart object itself with sessions tho.

    I know this was a very long post, so im very gratefull to any replies i get.
    Raymond Julin
    Developer: Hardware.no, Amobil.no, Gamer.no, Prisguide.no ...
    Owner: Kulturo.no

  2. #2
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by Findus
    How would you store the data between pages?
    I could pass the cart object in session vars.
    I could create temporary tables and pass the db connection in session vars (my inital thought).
    I could get the carts contet and pass it as a session var.
    You need to divide your data into persistent business data and (temporary) application data. This is clearly what you want to do conceptually and the right thing to do, but I think you are limiting yourself by assuming one has to be DB and the other Sessions.

    The first objective is to have two sets of data. It would be helpful if you could wrap the data sources in their own objects (called data accessors), but you can cross that bridge when you come to it. Clearly the business data goes into the DB as it has to last forever.

    The application/presentation data has a very different lifetime. You presumeably want abandoned carts to dissappear after 90 mins or so. Using sessions should be fine for this because they have lifetime built in. Unfortunately it can be tricky to manage those sessions globally, for example to monitor statistics.

    Another problem you will run into is trying to stash a DB connection into the session. The session would have to reconnect and all sorts of other problems would occour. Don't go down this route, but instead remove all of the DB access from the cart. That way it is just a container for data yes? That way yuo can safely serialise it. If the cart gets more complex then consider having a separate object for the contents that can be serialised (memento pattern). You should be able to recreate the cart by dropping this into the Cart constructor.

    Where you will ultimately end up I think is having the session maangement in the DB as well. Not mixed in with the main data, and possibly in a separate DB altogether. Same storage, but still conceptually apart.

    Here is a "full on" version. Suppose you had...
    PHP Code:
    class Cart {
        ...
        function 
    addItem($epos) { ... }
        function 
    removeItem($epos) { ... }
        function 
    getAll() { ... }
    }

    class 
    Checkout {
        function 
    Checkout(&$order_manager) { ... }
        function 
    unloadOrder($cart) { ... }
    }

    class 
    OrderManager {
        ...
        function 
    createOrder() { ... }
        function 
    dispatchOrder() { ... }
    }

    class 
    SessionPool() {
        function 
    getSession($cookie) { ... }
        function 
    instance() { ... }
    }

    class 
    Session {
        ...
        function 
    saveCart($cart) { ... }
        function 
    restoreCart($cart) { ... }

    The SessionPool handles the interactions with the PHP session system such as setting various handlers. Later you can add stitistics and stuff. The Session class just wraps the $_SESSION variable. Your top level script (or Action if you are framework inclined) would pull and push the Cart to the Session at the start and finish. This way the Session only talks to $_SESSION, the SessionPool talks to the session DB (or just the PHP $_SESSION system whilst prototyping). The OrderManager is the only one that talks to the database. The Cart is just a data holder and can be passed around, copied, etc. The calculations are done in the Checkout which uses the OrderManager as a service.

    This will sound way more complicated, but it is actually easier to deal with. Lot's of small simple objects rather than big do-everything ones.

    Just an idea .

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  3. #3
    SitePoint Zealot sanka69's Avatar
    Join Date
    Apr 2003
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Findus
    How would you store the data between pages?
    When I do this, I use standard session variables. and store and retrieve the cart by serialization of the classes:

    PHP Code:
      $cart unserialize($_SESSION['cart']);
       
    $_SESSION['cart'] = serialize($cart); 
    Quote Originally Posted by Findus
    I could create temporary tables and pass the db connection in session vars (my inital thought).
    I only do this when transferring the session accross domains. For example, using www.example.com, but you had to order from secure.example.com, I create a table with a session_id, and a large text field. Store the cart (via serialization) into the database, and pass the session id to the second page. It's a bit more involved (secureity measures etc), but that's the basic plot.

    Of course, that table could be used to reference the cart through the database, but this might be a performance hit depending on your setp. Also, I (think) it would benefit to use that approach if you're using multiple servers.

    Other suggestions with you're code? I only have two:

    1) Use a CartItem object to represent items. You could just use an array etc, but you get better abstraction when using items.

    PHP Code:

     
    class CartItem {
         function 
    CartItem($productId$qty) {
              
    $this->productId $productId
              
    //etc..
         
    }
         
    //..
     

    2) Seperate the createOrder into a seperate class, like an OrderContoller. ('Controller' might be incorrect, I still have learning issues with this terminology )

    This class would handle the conversion of the Cart into an Order. (And as the name implies, Cart and Order are seperate so it would make sense to seperate the functionality).

    Sorry if I'm not being very articulate in my reply, but hope this helps.
    Richard

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not fully read your reply Marcus,

    This will sound way more complicated, but it is actually easier to deal with.
    Not complicated at all, in fact it all makes sense Looking at the class structure just simplifies the whole process really

  5. #5
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Findus
    I need to create a simple shopping system for a site.
    After some looks at what allready exists i found out they are all to advanced,
    you can take a look at my shopping cart system.
    It's (intentionally) kept pretty simple and basic allowing others to modify it easily. (take a look at my tearline for the link)
    For the cart that's the only class i use

    PHP Code:
    class Cart {
    // private var $username
    var $username;
     
    // private var $userid
    var $userid;
    // [...]

    IMHO the cart class is not the right place for any userdata
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by frezno
    you can take a look at my shopping cart system.
    It's (intentionally) kept pretty simple and basic allowing others to modify it easily. (take a look at my tearline for the link)
    For the cart that's the only class i use

    IMHO the cart class is not the right place for any userdata
    Well, each cart instance needs to be coupled to a user. Its not for keeping user data, its for identifying who's cart it is.

  7. #7
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    UserID is ok (necessary) but imho there's no need for a username
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by frezno
    UserID is ok (necessary) but imho there's no need for a username
    Your right.
    The getUserID method should be in my user handling classes, or wherever i like to get it. But for now, in development of the cart i dont combine it with anything, so it will stay in there just for convinience, but will pull it out once i finalize it.

    Lastcraft:

    Your "full on" example makes sense, except one thing. Could you perhaps explain the SessionPool class a bit better, i dont really understand its point and thus not understand how to implement it either.

  9. #9
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Findus
    so it will stay in there just for convinience,
    yeah, i know this. Sometimes it's easier and faster to hack something together to see whether it's working.
    But what i've learned reading this forum here is, that pretty often it makes more sense (especially for OO ?) to split things up into smaller, logical chunks ([sub-] classes).

    And:
    but will pull it out once i finalize it.
    That's what i'm always saying, too
    But the reality is, that if it's finally working i let it be too often.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  10. #10
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by Findus
    Your "full on" example makes sense, except one thing. Could you perhaps explain the SessionPool class a bit better, i dont really understand its point and thus not understand how to implement it either.
    It is really just a tool to manage al of the sessions globally and to deal with some of the machinery of registering session handlers, etc. For example if you wanted to list all of the currently active carts on the system you need to query all of the sessions. You cannot use an individual session, becaue you don't want just one session.

    If you don't need this functionality, then obviously don't bother. I did say it was full on .

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  11. #11
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, a little update.

    I adapted marcus' idea an starting putting together something.
    Some questions had to arise ofc.
    Warning, its a bit long perhaps.

    First the code:

    The cart class:
    PHP Code:
    class Cart {
            
    // private var $items
            // Contains an array with items for the given cart
            
    var $items = array();

            
    // addItem function
            
    function addItem($itemid$quantity) {
                    if (isset(
    $itemid) && isset($quantity) && is_numeric($itemid) && is_numeric($quantity)) {
                            if (
    array_key_exists($itemid$this->items)) {
                                    
    $this->items[$itemid] = $this->items[$itemid] + $quantity;
                            }
                            else {
                                    
    $this->items[$itemid] = $quantity;
                            }
                    }
            }
            
            
    // deleteItem function
            
    function deleteItem($itemid$quantity) {
                    if (isset(
    $itemid) && isset($quantity) && is_numeric($itemid) && is_numeric($quantity)) {
                            if (
    $this->items[$itemid] <= $quantity) {
                                    unset(
    $this->items[$itemid]);
                            }
                            else {
                                    
    $this->items[$itemid] = $this->items[$itemid] - $quantity;
                            }
                    }
            }
            
            
    // getContent function
            
    function getContent() {
                    return 
    $this->items;
            }

    The ordermanager:
    PHP Code:
    class OrderManager {
            
    // private var $db
            
    var $db;
            
            
    // private var $items
            
    var $items = array();
            
            
    // private var $userid
            
    var $profileid;
            
            
    // private var $orders
            
    var $orders null;

            function 
    OrderManager($profileID) {
                    
    $this->profileid $profileID;
                    
    $this->db =& new MyDatabase('nude_shirt''localhost');
                    
    $this->db->connect('root''');
            }

            function 
    createOrder(&$cart) {
                    
    $sql "INSERT INTO orders (ID, Created, Sent, ReceiverID, Status)
                            VALUES ('', now(), '', '
    $this->profileid', '0')";
                    
    $query $this->db->query($sql);
                    if (
    $query->isSuccess() == true) {
                            
    $sql "SELECT ID FROM orders ORDER BY ID DESC LIMIT 1";
                            
    $result $this->db->query($sql);
                            
    $data $result->getRow(0ECLIPSE_DB_ASSOC);
                            
    $orderID $data['ID'];
                    }
                    
    $this->items $cart->getContent();
                    foreach (
    $this->items as $itemid => $quantity) {
                            
    $sql "INSERT INTO order_items (ID, OrderID, ItemID, Quantity)
                                    VALUES ('', '
    $orderID', '$itemid', '$quantity')";
                            
    $this->db->query($sql);
                    }
                    return 
    $orderID;
            }

            function 
    getOrderData() {
                    
    $sql "SELECT * FROM orders";
                    
    $result $this->db->query($sql);
                    for (
    $it =& new QueryIterator($result); $it->isValid(); $it->next()) {
                            
    $this->orders[] = $it->getCurrent();
                    }
                    return 
    $this->orders;
            }
            
            function 
    getOrder($id) {
                    
    $sql "SELECT * FROM orders WHERE ID LIKE $id";
                    
    $result $this->db->query($sql);
                    
    $order =& new Order($result->getRow(0));
                    
    $order->setContent(/* set content */);
                    return 
    $order;
            }

    And finaly the order class
    PHP Code:
    class Order {
            
    // private array $order_data
            
    var $order_data = array();
            
            
    // private array $content
            
    var $content = array();

            function 
    Order($order_data) {
                    
    $this->order_data $order_data;
            }
            
            
    // internal
            // used by ordermanager to set content??
            
    function setContent($content_data) {
                    
    $this->content $content_data;
            }
            
            function 
    getContent() {
                    return 
    $this->content;
            }
            
            function 
    getProfile() {
                    
    // return instance of profile class
            
    }
            function 
    setOrderStatus() {
                    
    // should the order class deal with db, or where should this be dealt with?
            
    }
            function 
    deleteOrder() {
                    
    // same question as above
            
    }

    My problem now is the order class.
    I use ordermanager to manage all orders, thus its used to retreive a singel order, or create a new order.
    Im uncertain about which class should be responsible for what.
    I need to be able to change the order, mainly order status when an actual order has been processed.
    But for the sake of simplicity i dont think i want to give the order class db access. That makes it hard to change the status.
    Should i allow order class to change status?
    Or should the ordermanager have a method for changing orders?
    Or are there another good way?

    My thought about the order class was for it to be a container which will be used for displaying orders to user and admin later.

    So if anyone would be so kind to give me some help here it would be appreciated.
    Also some general comments if im moving in a better direction here.

  12. #12
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Findus
    The cart class:
    PHP Code:
    class Cart {
    // addItem function
    // deleteItem function
    // getContent function

    You may want to add a method for changing the quantity.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  13. #13
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I proberly would make the OrderManager a Gateway (read about Pattern), as this is basically what it is, then have the Order class 'manage', including the status of an order, though you may want to explain more on this point, since I don't know what your doing to determine an orders status or not, ie how complicated is it ?

    As simple, as changing over a variable for example, I don't know

  14. #14
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    frezno:
    Its very simply, just deleteItem with how many you want to delete, if you remove all, then the item itself will be removed.

    Widow Maker:
    Quantity will just be a post in a db row, so its quite simple.
    Hmm, gateway pattern, couldnt find any good ressources on that pattern.
    Its not in the J2EE catalog. Well, found it in the msdn catalog. Perhaps you could give a quick description of it.
    Just looking at the name i'd say it's use is to act like a gateway between an object/app and for example a storage point, be it db or file or whatever.
    If that is so, its kind of what its turning into now.

  15. #15
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Findus
    frezno:
    Its very simply, just deleteItem with how many you want to delete, if you remove all, then the item itself will be removed.
    I realized that but there are also chances that the user may add to the former quantity.
    And for this i don't see any possibility with deleteItem.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  16. #16
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont get you.
    User add to former quantity? If quantity is 0, then obviously the item itself is not in the cart. So how could that be a problem?
    I could add another method, but i dont plan on adding anything i dont see myself using. But if you could explain it a bit better so i understand it might make sense for me

  17. #17
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the misunderstanding.
    What i mean is: how do you handle it if the user wants to change the quantity of an existing item in the cart?

    e.g. the cart shows 3 Cohiba Cigars but the user wants to change it to 5.

    I don't see this to be possible with neither addItem nor deleteItem. Or am i just to stupid to figure it out?
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  18. #18
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very simple, addItem to cart.
    addItem detects if the given productID is allready in the cart, and if so, it increments the quantity by the number given.
    You should not have to know if the item is in the cart allready to add it imo.
    I do think the cart only needs those 3 methods to do anything you want the cart itself to do.

  19. #19
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Findus
    addItem detects if the given productID is allready in the cart, and if so, it increments the quantity by the number given.
    So, if three are in the cart and i change the quantity to 5 then 5 will be added making a total of 8, which is obviously not correct.
    Sorry, i still don't get it.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  20. #20
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If quantity of item X is 5.
    And you do something like:
    $cart->addItem(X, 3);

    What would be the logical outcome? Item x containing 3, 5 or 8.
    Of course 8. Which it will.
    Same goes with delete.

  21. #21
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Findus
    If quantity of item X is 5.
    And you do something like:
    $cart->addItem(X, 3);
    Well, i think the logical procedure is that the user looks at his cart and e.g. there's a quantity of 5.
    Now, he doesn't want 5 but a total of 8.
    In the input field of his form his puts in an 8 for the desired quantity
    and not a 3 (the difference), i.e. you will get an addItem(X, 8); or not?
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  22. #22
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doh.
    I misunderstood what you meant.
    Yeah, from a users perspective he would want that in that case.
    You are probably right that i should add a changeQuantity method.
    Else i would have to first get the quantity and then calculate what to add, would be a bit wrong.
    Why is it that sometimes one cant just understand the most simple things, hmm....

  23. #23
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Findus
    Why is it that sometimes one cant just understand the most simple things, hmm....
    that's what happens to me with women, gives me sometimes a hard time to understand them
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  24. #24
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what your looking for, can explain things a lot more and better than I ever could

    http://www.martinfowler.com/eaaCatalog/

    Data Source Architectural Patterns ...
    There are basiclly 4 seperate patterns you'd need to look at, hope it helps

    Btw, if you don't already have the book, buy, borrow, beg or steal a copy

    Highly recommended, I don't have the refactoring book, but buy, borrow, beg or steal a copy of this as well

  25. #25
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, problem is that i dont have the book, *yet*.
    Im planning on getting it, but the combinatino low on cash, expensive book and summer isnt a nice one.
    Ah, i'll just finish the thing for now and refactor it later once my brain is a bit fatter


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •