【发布时间】:2020-10-19 09:10:34
【问题描述】:
我在我的 PHP 文件中创建了一个简单的购物车,我为此使用了会话,我能够完美地显示订单点击并显示在 cart.ph 中。现在,我为购物车页面中客户订购的每件商品添加了一个数量文本字段。现在我想要摘要页面,其中包含客户在他/她中输入的产品名称和数量,但显然我只能在购物车中获得一个数量正确的产品,其余的默认为 1 这是我的代码
这是我的购物车.ph
<?php
session_start();
$status="";
if (isset($_POST['action']) && $_POST['action']=="remove"){
if(!empty($_SESSION["shopping_cart"])) {
foreach($_SESSION["shopping_cart"] as $key => $value) {
if($_POST["code"] == $key){
unset($_SESSION["shopping_cart"][$key]);
$status = "<div class='box' style='color:red;'>
Product is removed from your cart!</div>";
}
if(empty($_SESSION["shopping_cart"]))
unset($_SESSION["shopping_cart"]);
}
}
}
if (isset($_POST['action']) && $_POST['action']=="change"){
foreach($_SESSION["shopping_cart"] as &$value){
if($value['code'] === $_POST["code"]){
$value['quantity'] = $_POST["quantity"];
break; // Stop the loop after we've found the product
}
}
}
?>
<html>
<head>
<title>SKIP-CART</title>
<link rel='stylesheet' href='css/style.css' type='text/css' media='all' />
</head>
<body>
<div style="width:700px; margin:50 auto;">
<h2>Shopping Cart</h2>
<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
?>
<div class="cart_div">
<a href="cart.php">
<img src="cart-icon.png" /> Cart
<span><?php echo $cart_count; ?></span></a>
</div>
<?php
}
?>
<div class="cart">
<?php
if(isset($_SESSION["shopping_cart"])){
$total_price = 0;
?>
<table class="table">
<tbody>
<tr>
<td></td>
<td>ITEM NAME</td>
<td>QUANTITY</td>
</tr>
<?php
foreach ($_SESSION["shopping_cart"] as $product){
?>
<tr>
<td><img src='<?php echo $product["image"]; ?>' width="50" height="40" /></td>
<td><?php echo $product["name"]; ?><br />
<form method='post' action=''>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>" />
<input type='hidden' name='action' value="remove" />
<button type='submit' class='remove'>Remove Item</button>
</form>
</td>
<td>
<form method='post' action='display.php' id='orderform'>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>" />
<input type='hidden' name='action' value="change" />
<input type="text" id="quantity" name="quantity">
</select>
</form>
</td>
<td></td>
<td></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="right">
<strong><button type="submit" form="orderform" value="submit">Proceed Order</button></strong>
</td>
</tr>
</tbody>
</table>
<?php
}else{
echo "<h3>Your cart is empty!</h3>";
}
?>
</div>
<div style="clear:both;"></div>
<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>
<br/><br />
</div>
</body>
</html>
这是我的 display.php 此文件将获取购物车中的所有产品和客户/用户编辑的数量,但显然我只能根据 cart.php 显示 1 个正确的数量记录
<?php
session_start();
$status="";
if (isset($_POST['action']) && $_POST['action']=="remove"){
if(!empty($_SESSION["shopping_cart"])) {
foreach($_SESSION["shopping_cart"] as $key => $value) {
if($_POST["code"] == $key){
unset($_SESSION["shopping_cart"][$key]);
$status = "<div class='box' style='color:red;'>
Product is removed from your cart!</div>";
}
if(empty($_SESSION["shopping_cart"]))
unset($_SESSION["shopping_cart"]);
}
}
}
?>
<html>
<head>
<title>SKIP-CART</title>
<link rel='stylesheet' href='css/style.css' type='text/css' media='all' />
</head>
<body>
<div style="width:700px; margin:50 auto;">
<h2>Shopping Cart</h2>
<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
?>
<div class="cart_div">
<a href="cart.php">
<img src="cart-icon.png" /> Cart
<span><?php echo $cart_count; ?></span></a>
</div>
<?php
}
?>
<div class="cart">
<?php
if(isset($_SESSION["shopping_cart"])){
$total_price = 0;
?>
<table class="table">
<tbody>
<tr>
<td></td>
</tr>
<?php
foreach ($_SESSION["shopping_cart"] as $product){
?>
<tr>
<td><img src='<?php echo $product["image"]; ?>' width="50" height="40" /></td>
<td><?php echo $product["name"]; ?><br />
</td>
<td><?php echo $product["quantity"]; ?><br />
</td>
<td></td>
<td></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="right">
<strong><button type="submit" form="orderform" value="submit">Proceed Order</button></strong>
</td>
</tr>
</tbody>
</table>
<?php
}else{
echo "<h3>Your cart is empty!</h3>";
}
?>
</div>
<div style="clear:both;"></div>
<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>
<br/><br />
</div>
</body>
</html>
这是购物车的示例图片:
当我点击继续订单时,这是我的 display.php
【问题讨论】:
-
您的“Proceed Order”提交按钮似乎不属于任何这些表单的一部分,所以我看不出您将如何获取甚至 one的数据> 提交的产品...?
-
我从我创建的名为 shopping_cart 的会话中获取数据,因此我可以显示我创建的会话中的数据,但我无法在 cart.php 的摘要页面基础中显示数量我只能显示1 个正确数量