如何保留以前的 echo 值并在 php 中回显它下面的另一个值?
how to retain the previous echo value and echo another one below it in php?
我正在创建购物车,但我的回显没有按预期出现。我不确定我哪里出错了。
这是我的产品详情页面,
<form action="shoppingCart.php?addToCart=<?php echo $key ?>" method="post">
<select name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="hidden" name="productName" value="<?php echo $key ?>">
<input type="submit" name="addToCart" value="Add to cart" class="btn btn-primary">
</form>
这是我的购物车php代码:
<?php
session_start();
if (isset($_SESSION['shoppingCart'])) {
$_SESSION['shoppingCart'] = [];
}
if (isset($_POST['addToCart'])) {
echo "Product: " . $_POST['productName'] . "<br/>";
$count = count($_SESSION['shoppingCart']);
echo "count: $count<br/>";
$_SESSION['shoppingCart'][$count]['productName'] = $_POST['productName'];
$_SESSION['shoppingCart'][$count]['quantity'] = $_POST['quantity'];
echo "<h3>Your Cart</h3>";
if (empty($_SESSION['shoppingCart'])) {
echo "your cart is empty<br/>";
} else {
foreach ($_SESSION['shoppingCart'] as $key => $names) {
echo "cart($key) = product: " . $names['productName'] . "-Quantity: " . $names['quantity'] . "<br/>";
}
}
}
?>
我只能在添加到购物车页面中回应这个。
产品:考拉
计数:0
您的购物车
购物车(0) = 产品:AkwardKoala-数量:1
我怎样才能做到这一点,
您的购物车
购物车(0) = 产品:考拉-数量:1
购物车(1) = 产品:猫-数量:3
购物车(2) = 产品:鱼-数量:1
这是错误的:
if (isset($_SESSION['shoppingCart'])) {
$_SESSION['shoppingCart'] = [];
}
您正在清空已设置的会话变量。我想你想在 not 设置时初始化它。所以应该是:
if (!isset($_SESSION['shoppingCart'])) {
$_SESSION['shoppingCart'] = [];
}
然后您可以将新商品添加到购物车,所有旧商品将被保留。
我正在创建购物车,但我的回显没有按预期出现。我不确定我哪里出错了。
这是我的产品详情页面,
<form action="shoppingCart.php?addToCart=<?php echo $key ?>" method="post">
<select name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="hidden" name="productName" value="<?php echo $key ?>">
<input type="submit" name="addToCart" value="Add to cart" class="btn btn-primary">
</form>
这是我的购物车php代码:
<?php
session_start();
if (isset($_SESSION['shoppingCart'])) {
$_SESSION['shoppingCart'] = [];
}
if (isset($_POST['addToCart'])) {
echo "Product: " . $_POST['productName'] . "<br/>";
$count = count($_SESSION['shoppingCart']);
echo "count: $count<br/>";
$_SESSION['shoppingCart'][$count]['productName'] = $_POST['productName'];
$_SESSION['shoppingCart'][$count]['quantity'] = $_POST['quantity'];
echo "<h3>Your Cart</h3>";
if (empty($_SESSION['shoppingCart'])) {
echo "your cart is empty<br/>";
} else {
foreach ($_SESSION['shoppingCart'] as $key => $names) {
echo "cart($key) = product: " . $names['productName'] . "-Quantity: " . $names['quantity'] . "<br/>";
}
}
}
?>
我只能在添加到购物车页面中回应这个。
产品:考拉
计数:0
您的购物车
购物车(0) = 产品:AkwardKoala-数量:1
我怎样才能做到这一点,
您的购物车
购物车(0) = 产品:考拉-数量:1
购物车(1) = 产品:猫-数量:3
购物车(2) = 产品:鱼-数量:1
这是错误的:
if (isset($_SESSION['shoppingCart'])) {
$_SESSION['shoppingCart'] = [];
}
您正在清空已设置的会话变量。我想你想在 not 设置时初始化它。所以应该是:
if (!isset($_SESSION['shoppingCart'])) {
$_SESSION['shoppingCart'] = [];
}
然后您可以将新商品添加到购物车,所有旧商品将被保留。