php - 如何使用 php 编辑购物车中的数量框?

标签 php mysql

我使用 php 和 mysql 创建了小型购物车页面。

所有产品都存储在数据库中。现在我的页面看起来像这样 http://s13.postimg.org/fsapl50hj/Untitled_1_copy.png .

页面顶部,数量框中。我需要编辑该框,因为如果客户在添加到购物车后想要更改(增加或减少)数量,

这是我的 php 代码:

<?php
session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
    case "add":
        if(!empty($_POST["quantity"])) {
            $productByCode = $db_handle->runQuery("SELECT * FROM tblproduct WHERE code='" . $_GET["code"] . "'");
            $itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));

            if(!empty($_SESSION["cart_item"])) {
                if(in_array($productByCode[0]["code"],$_SESSION["cart_item"])) {
                    foreach($_SESSION["cart_item"] as $k => $v) {
                            if($productByCode[0]["code"] == $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["code"] == $k)
                        unset($_SESSION["cart_item"][$k]);              
                    if(empty($_SESSION["cart_item"]))
                        unset($_SESSION["cart_item"]);
            }
        }
    break;
    case "empty":
        unset($_SESSION["cart_item"]);
    break;  
}
}
?>
<HTML>
<HEAD>
<TITLE>Simple PHP Shopping Cart</TITLE>
<link href="style.css" type="text/css" rel="stylesheet" />
</HEAD>
<BODY>
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" href="index.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>Code</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th><strong>Action</strong></th>
</tr>   
<?php       
    foreach ($_SESSION["cart_item"] as $item){
        ?>
                <tr>
                <td><strong><?php echo $item["name"]; ?></strong></td>
                <td><?php echo $item["code"]; ?></td>
                <td><?php echo $item["quantity"]; ?></td>
                <td align=right><?php echo "$".$item["price"]; ?></td>
                <td><a href="index.php?action=remove&code=<?php echo $item["code"]; ?>" 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 "$".$item_total; ?></td>
</tr>
</tbody>
</table>    

  <?php
}
?>

谁能帮我解决这个问题,先谢谢了。

最佳答案


在您的情况下,您需要使用另一个带有输入框的表单来检索用户的数量,Html5 有一个输入类型数字(用户只能添加数字):

<input type="number" name="quantity" min="1" max="50" value="<?php echo $item["quantity"]; ?>">
- 但它是针对 html5

对于 Xhtml 和 HTML4.01,您可以使用:

<input type="text" value="<?php echo $item["quantity"]; ?>" name="quantity">

您还需要通过 php 使用“is_numeric”函数进行简单的数字验证。

希望能帮上忙

关于php - 如何使用 php 编辑购物车中的数量框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29159832/

相关文章:

php - Swift - 在应用程序内购买 - 从 mysql 数据库加载产品

java - 尝试将数据库中的信息显示到终端中

PHP 中使用 Switch 的 JavaScript 脚本

php - 在 PHP 数组中计算年龄

php - 如果mysql的php中的获取值为0,如何设置变量?

javascript - 需要有关数据库结构的建议

php - Eloquent 多行插入并在一个数组循环中

php - 为什么socket read不离开循环?

php - Laravel Eloquent SQL 查询与 OR 和 AND 与 where 子句

php - MySQL 更新 "link"表