SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Can't increment product's quantity in session object

    I created an object, it's named carwash. I created a session and assigned that object for session, if I enter quantity then and press buy button, I have result (example 6):

    Your shopping cart contains 6 items.
    but when I enter nothing, I get:

    Your shopping cart contains items.
    How should I do? Thank you! Here is all my code:

    index.php
    PHP Code:
    <?php
        session_start
    (); 
        
        require_once 
    'carwash.php';
        if (isset(
    $_SESSION['encoded_cartopass'])) {
            
    // first let's get the variable from the session
            
    $encoded_cartopass $_SESSION['encoded_cartopass'];
            
            
    // now let's unpack it
            
    $cartopass unserialize($encoded_cartopass);
            
            
    // echo quantity
            
    echo "Your shopping cart contains {$cartopass->getQuantity()} items. <br /><br />";
        }
        else {
            echo 
    "Your shopping cart contains 0 items. <br /><br />";
        }
    ?>

    <form action="process.php" method="post">
        Quantity: <input type="text" name="quantity" id="quantity"/>
        <input type="submit" value="Buy" name="submit" />
    </form>
    process.php
    PHP Code:
    <?php

        
    require_once 'carwash.php';

        if (isset(
    $_POST['submit'])) {
            
            if (!isset(
    $_SESSION['encoded_cartopass'])) {
                
                
    // construct and set quantity
                
    $cartopass = new carwash();
                
    $cartopass->setQuantity($_POST['quantity']);
                
                
    // construct and encode session
                
    session_register('encoded_cartopass');
                
    $_SESSION['encoded_cartopass'] = serialize($cartopass);
            }
            else {
                
    // if session is existing, decode it and
                // increment quantity
                
    $encoded_cartopass $_SESSION['encoded_cartopass'];
                
                
    $cartopass unserialize($encoded_cartopass);
                
                
    $cartopass->incrementQuantity($_POST['quantity']);
                
                
    // encode class and assign to session and
                // session is used pass to index.php
                
    $_SESSION['encode_cartopass'] = serialize($cartopass);
            }
            
        }
        
        echo 
    "<script>window.location.href='index.php'</script>";
    ?>
    carwash.php
    PHP Code:
    <?php
        
    class carwash {
            
            private 
    $carmake;
            private 
    $caryear;
            private 
    $quantity;
            
            public function 
    getCarmake() {
                return 
    $this->carmake;
            }
            
            public function 
    setCarmake($carmake) {
                
    $this->carmake $carmake;
            }
            
            public function 
    getCaryear() {
                return 
    $this->caryear;
            }
            
            public function 
    setCaryear($caryear) {
                
    $this->caryear $caryear;
            }
            
            public function 
    getQuantity() {
                return 
    $this->quantity;
            }
            
            public function 
    setQuantity($quantity) {
                
    $this->quantity $quantity;
            }
            
            public function 
    incrementQuantity($quantity '') {
                if (empty(
    $quantity)) {
                    
    $this->quantity++;
                }
                else {
                    
    $this->quantity += $quantity;
                }
            }
            
            public function 
    washcar() {
                echo 
    "scruba, dub, dub, scruba, dub, dub <br />";
                echo 
    "I'm feelling cleaner, Thank you!";
            }
        }
    ?>

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The if statement is the problem:
    PHP Code:
    <?php 
        
    if (isset($_SESSION['encoded_cartopass'])) { 
            
    // echo quantity 
            
    echo "Your shopping cart contains {$cartopass->getQuantity()} items. <br /><br />"
        } 
        else { 
            echo 
    "Your shopping cart contains 0 items. <br /><br />"
        } 
    ?>
    $_SESSION['encoded_cartopass']) is set but it contains an empty (not set) $cartopass->getQuantity()
    Denny Schlesinger
    web services

  3. #3
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    The if statement is the problem:
    PHP Code:
    <?php 
        
    if (isset($_SESSION['encoded_cartopass'])) { 
            
    // echo quantity 
            
    echo "Your shopping cart contains {$cartopass->getQuantity()} items. <br /><br />"
        } 
        else { 
            echo 
    "Your shopping cart contains 0 items. <br /><br />"
        } 
    ?>
    $_SESSION['encoded_cartopass']) is set but it contains an empty (not set) $cartopass->getQuantity()
    I found in Java document (and ASP.NET) that session can store an object but I just knew, in PHP, it's limited. So, I use encode and decode aproach to passing object to another page. I did, thank you.


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
  •