dqwh1209 2016-08-20 10:26
浏览 35
已采纳

PHP中的购物车中没有显示总价格

I have a shopping cart page which the total price didn't show up in the table the products added (picture below). I can't figure out where's the error. I need help, thanks in advance!

enter image description here

Here's my code:

example.php

<?php include("dbconnection.php");?>
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Meal</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
    <div class="container" style="width:60%;">
        <h2 align="center">SoftAOX Tutorial | Creating an Online Shopping Cart in PHP & Mysql</h2>
        <?php
            $query = "SELECT * FROM meal ORDER BY meal_id ASC";
            $result = mysqli_query($con, $query);
            if(mysqli_num_rows($result) > 0) {
                while($row = mysqli_fetch_array($result)) {
        ?>
                    <div class="col-md-3">
                        <form method="post" action="shop.php?action=add&id=<?php echo $row["meal_id"]; ?>">
                            <div style="border: 1px solid #eaeaec; margin: -1px 19px 3px -1px; box-shadow: 0 1px 2px rgba(0,0,0,0.05); padding:10px;" align="center">
                                <img src="<?php echo $row["meal_image_Upload"]; ?>" class="img-responsive">
                                <h5 class="text-info"><?php echo $row["meal_name"]; ?></h5>
                                <h5 class="text-danger">$ <?php echo $row["meal_price"]; ?></h5>
                                <input type="text" name="quantity" class="form-control" value="1">
                                <input type="hidden" name="hidden_name" value="<?php echo $row["meal_name"]; ?>">
                                <input type="hidden" name="hidden_price" value="<?php echo $row["meal_price"]; ?>">
                                <input type="submit" name="add" style="margin-top:5px;" class="btn btn-default" value="Add to Bag">
                            </div>
                        </form>
                    </div>
        <?php
                }
            }
        ?>
        <div style="clear:both"></div>
        <h2>My Shopping Bag</h2>
        <div class="table-responsive">
            <table class="table table-bordered">
                <tr>
                    <th width="40%">Product Name</th>
                    <th width="10%">Quantity</th>
                    <th width="20%">Price Details</th>
                    <th width="15%">Order Total</th>
                    <th width="5%">Action</th>
                </tr>
                <?php
                    if(!empty($_SESSION["cart"])) {
                        $total = 0;
                        foreach($_SESSION["cart"] as $keys => $values) {
                ?>
                            <tr>
                                <td><?php echo $values["item_name"]; ?></td>
                                <td><?php echo $values["item_quantity"] ?></td>
                                <td><?php echo $values["product_price"]; ?></td>
                                <td><?php echo number_format(($values["item_quantity"] * $values["product_price"]), 2); ?></td>
                                <td><a href="shop.php?action=delete&id=<?php echo $values["product_id"]; ?>"><span class="text-danger">X</span></a></td>
                            </tr>
                <?php 
                            $total = $total + ($values["item_quantity"] * $values["product_price"]);
                        }
                ?>
                        <tr>
                            <td colspan="3" align="right">Total</td>
                            <td align="right"><?php echo number_format($total, 2); ?></td>
                            <td></td>
                        </tr>
                <?php
                    }
                ?>
            </table>
        </div>
    </div>
</body>
</html>

shop.php

<?php
       include("dbconnection.php"); 

       if(isset($_POST["add"])) {
           if(isset($_SESSION["cart"])) {
               $item_array_id = array_column($_SESSION["cart"], "product_id");
               if(!in_array($_GET["id"], $item_array_id)) {
                   $count = count($_SESSION["cart"]);
                   $item_array = array(
                       'product_id' => $_GET["id"],
                       'item_name' => $_POST["hidden_name"],
                       'product_price' => $_POST["hidden_price"],
                       'item_quantity' => $_POST["quantity"]
                   );
                   $_SESSION["cart"][$count] = $item_array;
                   echo '<script>window.location="example.php"</script>';
               } else {
                   echo '<script>alert("Products already added to cart")</script>';
                   echo '<script>window.location="example.php"</script>';
               }
           } else {
                   $item_array = array(
                       'product_id' => $_GET["id"],
                       'item_name' => $_POST["hidden_name"],
                       'product_price' => $_POST["hidden_price"],
                       'item_quantity' => $_POST["quantity"]
                   );
                   $_SESSION["cart"][0] = $item_array;
           }
       }
       if(isset($_GET["action"])) {
           if($_GET["action"] == "delete") {
               foreach($_SESSION["cart"] as $keys => $values) {
                   if($values["product_id"] == $_GET["id"]) {
                       unset($_SESSION["cart"][$keys]);
                       echo '<script>alert("Product has been removed")</script>';
                       echo '<script>window.location="example.php"</script>';
                   }
               }
           }
       }
  ?>
  • 写回答

3条回答 默认 最新

  • dongsisui7562 2016-08-20 11:14
    关注

    If you want to find out the float no from product price, consider using regex. This is the way u could do it

     $productprice = "RM6.90";
     preg_match('/([0-9]+\.[0-9]+)/', $productprice, $matches);
     $floatproductprice = (float)$matches[0];// it will contain the float value
    

    $floatproductprice should be used to calculate the total and also this value should be stored in the database. Also change the type of price details and order total in database to Float or Double.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch