我创建了一个对象,它名为carwash。我创建了一个会话并为会话分配了该对象,如果我输入数量然后按下购买按钮,我有结果(例6):
您的购物车包含6件商品。
但是当我什么也没输入时,我得到了:
您的购物车包含商品。
我该怎么办?谢谢!这是我的所有代码:
index.php
PHP代码:
<?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代码:
<?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代码:
<?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!";
}
}
?>
答案 0 :(得分:0)
编辑:(发现大括号的作用......) 以下任何一种都应该有效:
echo "Your shopping cart contains ".($cartopass->getQuantity() ? $cartopass->getQuantity() : 0)." items. <br /><br />";
echo "Your shopping cart contains ", $cartopass->getQuantity() + 0, " items. <br /><br />";
答案 1 :(得分:0)
有两种可能的解决方案 -
检查会话的值是否为"0"
(即使“0”为假,$v = "0"; isset($v)
将返回true)。
$_POST
为"0"
,则销毁会话。
<?php
session_start();
require_once 'carwash.php';
if (isset($_SESSION['encoded_cartopass']) && $_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 />";
}
?>
<?php
require_once 'carwash.php';
if (isset($_POST['submit']) && $_POST['quantity']) {
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);
}
} else {
unset($_SESSION['cartopass']);
session_destroy();
}
echo "<script>window.location.href='index.php'</script>";
?>