php图表项无法添加数量

时间:2016-05-03 14:28:33

标签: php session shopping-cart

大家都叫Fadil Raditya 我被困在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;

我想要在点击添加图表按钮时添加数量

<?php
    $product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id DESC");
    if (!empty($product_array)) { 
        foreach($product_array as $key=>$value){
    ?>
    <div class="col-xs-6 col-sm-3 col-md-3">
            <div class="team-wrapper-big">

            <form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">

            <h6><?php echo $product_array[$key]["name"]; ?></h6>
            <img src="<?php echo $product_array[$key]["image"]; ?>">
            <?php echo "Rp ".$product_array[$key]["price"]; ?>
            <hr>
            <a href="<?php echo $product_array[$key]["link"]; ?>" class="btn btn-circle" id="btn-scroll" target="_blank"><img src="img/VIEW.png"></a>

            <input type="text" name="quantity" value="1" placeholder="quantity in number" />
            <input type="submit" value="Add to cart" class="btnAddAction" src="img/BUY.png">
            <hr>
            </form>

        </div>
        </div>
    <?php
            }
    }
    ?>

这是输出源

<div id="shopping-cart">
<div class="txt-heading"></div>
<?php
if(isset($_SESSION["cart_item"])){
    $item_total = 0;
?><center>  
<table cellpadding="10" cellspacing="3" border="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>Total 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>
                <?php $_SESSION["track"] = $item["quantity"]; ?>
                <td align=right><?php echo "Rp. ".$item["price"]; ?></td>
                <td align=right><?php echo "Rp. ".$item["quantity"]*$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="6" align=right><strong>Total:</strong> <?php echo "Rp. ".$item_total; ?></td>
</tr>
</tbody>
</table>    </center>   
  <?php
}
?><a id="btnEmpty" href="index.php?action=empty">Empty Cart</a>
</div>

我希望有人能帮助我。需要建议和解决方案。开放讨论。

问候 Fadil Raditya

0 个答案:

没有答案