SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Cart Problem

  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cart Problem

    Hi all, I've written a shopping cart class but seems to be having a couple of problems with it. See below for the class:

    Code PHP:
    <?php
     
    class ShoppingCart {
     
        protected $items = array();
     
     
        public function is_empty(){
            if(empty($this->items)):
                return true;
            else:
                return false;
            endif;
        }
     
        //Method for adding an item to the cart...
        public function add_items($id, $info){
     
            //Is it already in the cart?
            if(isset($this->items[$id])):
     
                //Call the update_item() method:
                $this->update_item($id, $this->items[$id]['qty']+1);
     
            else:
     
                //Add the array of info...
                $this->items[$id] = $info;
     
                //Add the quantity...
                $this->items[$id]['qty'] = 1;
     
                //Print a message...
                echo "Item has been added successfully!";
     
            endif;
        }
     
        public function update_item($id, $qty){
     
            //Delete if quantity equals 0...
            if($qty == 0):
                $this->delete_item($id);
     
            elseif(($qty > 0) && ($qty != $this->items[$id]['qty'])):
     
                //Update the quantity...
                $this->items[$id]['qty'] = $qty;
     
                //Print a message...
                echo "You have now $qty of your chosen product.";
     
     
            endif;
     
        }
     
        //Method for deleteing an item...
        public function delete_items($id){
     
            //Confirm this isn't in the cart...
            if(isset($this->items[$id])):
                echo "This item has now been removed from your cart!";
                //Remove the item...
                unset($this->items[$id]);
            endif;
     
        }
     
    }
     
    ?>

    I then use the class in the following way, I've already included the class and created a new object:

    Code PHP:
    //Section to add an item to the cart!
    if(isset($_POST['cartadd'])):
    $bag->add_items($_POST['prodId'], "Hello");
    $_SESSION['cart'] = serialize($bag);
    endif;

    Then when I do the following the following is produced:

    Code PHP:
    print_r(unserialize($_SESSION['cart']));
     
    ShoppingCart Object ( [items:protected] => Array ( [368] => 2ello [19] => 1ello ) )

    Any ideas why the 'H' is missing? Any help/advice is appreciated

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    It's going wrong here:

    PHP Code:
    $this->items[$id] = $info;
    //Add the quantity...
    $this->items[$id]['qty'] = 1
    First you tell $this->items[$id] to be a string (the value of $info), then afterwards you tell it it's an array. PHP kinda borks up, and assumes (I think) that $this->items[$id]['qty'] is part of the string, and replaces the first letter with 1.

    Makes sense? Not really, but that's how PHP treats strings being accessed as an array.

    What you probably want to do is:

    PHP Code:
    $this->items[$id]['info'] = $info;
    //Add the quantity...
    $this->items[$id]['qty'] = 1
    That will give you a true array.

    Code:
    array( 368 => 
        array(
            info => Hello,
            qty => 2
        )
    )

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey dude, yeah that was the problem, fixed it all up and working perfectly. Cheers for the help


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
  •