使用PHP的购物车

时间:2015-11-11 06:21:21

标签: 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>

1 个答案:

答案 0 :(得分:0)

答案:

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的子数组。我提供的代码将检查$_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。

这样做的好处是即使用户退出,当该用户返回时,他/她仍然可以看到他/她放入他/她的购物车中的书籍。