gpt4 book ai didi

php - 使用 PHP 的购物车

转载 作者:行者123 更新时间:2023-11-30 22:31:58 25 4
gpt4 key购买 nike

enter image description here

我正在尝试制作一个购物车。我可以将书籍添加到购物车并清空整个购物车。但我无法删除单个购物车项目。我可以将一个项目添加到购物车并使用删除项目超链接将其删除。但是之后添加多个项目,我无法使用超链接删除项目。我该怎么办?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> </title>
<link rel="stylesheet" type="text/css" href="header.css" />

</head>

<body>
<div id="header">
<?php include './menu.php';?>
</div>


<div id="navigator">
</div>

<div id="section">

<?PHP
$name=$_SESSION['userName'];
$email=$_SESSION['myEmail'];
require("connection.php");


?><h2>
Welcome <?PHP echo($name); ?>,</h2>
<?PHP require("menu2.php"); ?><hr>

<?PHP

require("connection.php");
$Query=("select * from tb_book");
$result=mysql_query($Query);
?>

<?php
if(!empty($_GET["action"])) {
switch($_GET["action"]) {

case "add":
if(!empty($_POST["quantity"])) {
$result = mysql_query("SELECT * FROM tb_book WHERE bookID='" . $_GET["bookID"] . "'");
$productByCode=mysql_fetch_array($result);

$itemArray = array($productByCode["bookID"]=>array('bName'=>$productByCode["bName"], 'bookID'=>$productByCode["bookID"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode["price"]));


//$itemArray = array($productByCode[0]["bookID"]=>array('bName'=>$productByCode[0]["bName"], 'bookID'=>$productByCode[0]["bookID"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));

if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode["bookID"],$_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode["bookID"] == $k)
$_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($_GET["bookID"] == $k)
unset($_SESSION["cart_item"][$k]);
//if(empty($_SESSION["cart_item"]))
// unset($_SESSION["cart_item"]);
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}
?>



<table border="1" width="100%" height="100%">
<tr>


<td width="70%">
<div id="product-grid">
<div class="txt-heading">Products</div>
<?php
$product = mysql_query("SELECT * FROM tb_book ORDER BY bName ASC");
while($row=mysql_fetch_array($product)) {

?>
<div class="product-item">
<form method="post" action="buyBook.php?action=add&bookID=<?php echo $row["bookID"]; ?>">
<div><img src="./books/<?PHP echo($row['image']); ?>" height="100" width="100" /></div>
<div><?php echo $row["bName"]; ?></div>
<div class="product-price"><?php echo "INR &nbsp;".$row["price"]; ?></div>
<div><input type="text" name="quantity" value="1" size="2" />
<input type="submit" value="Add to cart" class="btnAddAction" /></div>
</form>
</div>
<?php
}

?>
</div>
</td>

<td width="30%" valign="top">


<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" href="buyBook.php?action=empty">Empty Cart</a></div>
<?php
if(isset($_SESSION["cart_item"])){
$item_total = 0;
?>
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>

<th><strong>Name</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th></th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td><?php echo $item["bName"]; ?></td>

<td><?php echo $item["quantity"]; ?></td>
<td align=right><?php echo "INR ".$item["price"]; ?></td>
<td><a href="buyBook.php?action=remove&bookID=<?php echo $item["bookID"]; ?>" class="btnRemoveAction">Remove Item</a></td>
</tr>
<?php
$item_total += ($item["price"]*$item["quantity"]);
}
?>
<tr>
<td colspan="5" align=right><strong>Total:</strong> <?php echo "INR ".$item_total; ?></td>
</tr>

</tbody>
</table>
<?php
}
?>
</div>
</table>
<br /><br /><center>
<form name="checkout" action="buyBook_action.php" method="post">
<input type="submit" value="PROCEED" />
</form>
</center>
</td>

</tr>
</table>




</div>


</body>
</html>

最佳答案

回答:

if (!empty($_GET["bookID"])) {

foreach($_SESSION["cart_item"] as $subKey => $subArray){

if($subArray["bookID"] == $_GET["bookID"]){ /* CHECK IF THERE IS A BOOKID THAT HAS THE SAME $_GET["bookID"] */
unset($_SESSION["cart_item"][$subKey]);
}

} /* END OF FOREACH */

}

您将包含 bNamebookIDquantityprice 的子数组存储在您的数组中 session 变量。我提供的代码将检查 $_GET["bookID"] 是否在 bookID 的子数组中。如果确实找到了一个,它将删除该组数组。

[
{"bName":"Physics","bookID":"1","quantity":"1","price":"1100.00"},
{"bName":"Algebra","bookID":"2","quantity":"2","price":"1200.00"},
{"bName":"Calculus","bookID":"3","quantity":"3","price":"1300.00"}
]

推荐

创建一个额外的表。让我们将其命名为 cart_table

cart_id  |  userID  |  bookID  |
---------+----------+----------+
1 | 1 | 1 |
2 | 1 | 2 |
3 | 1 | 3 |

userID 列是用户的 ID,bookID 列是用户放入购物车的图书的 ID。

这样做的好处是,即使用户注销,当该用户返回时,他/她仍然能够看到他/她放入购物车的书籍。

关于php - 使用 PHP 的购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33645258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com