php - 使用 PHP 的购物车

标签 php mysql

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/

相关文章:

php - 用于关键任务的 php HTTP_HOST 和 HTTP_REFERER 的可靠性

php - Strip_tags() 期望参数 1 为字符串,给定数组

PHP Response 直接来自 C/C++ 语言

php - Angular 中的多维数组有问题

带有修剪的mysql concat

php - 错误#2007,在AS3中,但它跟踪正确的结果,当添加var文本字段时,它在动态文本框中显示Null

php - 查询查询查询

php - 如果日期相等,Array_multisort 将失败

MySQL 查询匹配别名 WHERE 不起作用

mysql - 在 Red Hat 上安装 Maria DB (mysql)。安装后如何启动服务