doujiyan0031 2013-03-13 03:12
浏览 26

无法在会话对象中增加产品的数量

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条回答 默认 最新

  • dongyun8891 2013-03-13 03:24
    关注

    EDITED: (Found out what the curly braces do...) Either of the following should work:

    echo "Your shopping cart contains ".($cartopass->getQuantity() ? $cartopass->getQuantity() : 0)." items. <br /><br />"; 
    
    echo "Your shopping cart contains ", $cartopass->getQuantity() + 0, " items. <br /><br />"; 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?