代码之家  ›  专栏  ›  技术社区  ›  Chrissa

如何取消设置会话项计数

  •  0
  • Chrissa  · 技术社区  · 6 年前

    请帮忙,我正在练习建立一个电子商务网站,我希望在他们成功订购产品后,购物车图标中的项目计数将返回0并停止所有会话。假设我订购了3个产品,那么购物车将有3个项目徽章,完成后返回0。它确实返回到0,但当您单击要加载的购物车图标以加载\cart.php时,它仍将加载到我订购的上一个项目。

    checkout_action.php >>>>>going to header("location:order_confirmation.php?checkout=success");

    <?php session_start();
      if ($_GET['checkout'] =='success'){
      unset($_SESSION['item_count']);
     }
    ?>
    

    这是我存放手推车的地方 add_to_cart.php

    <?php session_start();
      $id = $_POST['item_id'];
      $quantity = $_POST['item_quantity'];
      $_SESSION["cart"][$id] = $quantity;
      $_SESSION["item_count"] = array_sum($_SESSION["cart"]);
      echo "<span class='carttxt glyphicon glyphicon-shopping-cart'> SHOP </span> 
         <span class='badge'>" . $_SESSION["item_count"] . "</span>";
    

    我用这个LOAD_cart.php加载购物车

     <?php
     session_start();
     include("includes/head.php");
     include("includes/db_config.php");
     include("includes/jsbottom.php");
    
     $data = "<table class='table table-striped table-responsive'>
       <thead>
         <tr>
           <th>Product</th>
           <th>Price</th>
           <th>Quantity</th>
           <th>Sub-Total</th>
           <th>Action</th>
           </tr>
         </thead>
       <tbody>";
    
    $grand_total = 0;
    foreach($_SESSION['cart'] as $product_id => $quantity) {
    $sql = "SELECT * FROM items where id = '$product_id' ";
    $result = mysqli_query($conn, $sql);
    if(mysqli_num_rows($result) > 0) {
       while($row = mysqli_fetch_assoc($result)) {
         $product_name = $row["product_name"];
         $product_desc = $row["product_desc"];
         $price = $row["price"];
         $image = $row["image"];
    
         //For computing the sub total and grand total
         $subTotal = $quantity * $price;
         $grand_total += $subTotal;
    
         $data .="<tr>
                 <td>$product_name . <div class='middle'> <img 
                src='$image' style='height:100px;width:100px;'/> </div> </td>
                <td id='price$product_id'> $price</td>
                <td><input type='number' class ='form-control' 
               value = '$quantity' id='quantity$product_id' 
               onchange='changeNoItems($product_id)' min='1' size='5'></td>
               <td id='subTotal$product_id'>$subTotal</td>
               <td><button class='btn btn-danger' 
               onclick='removeFromCart($product_id)'><span class='glyphicon 
               glyphicon-trash' aria-hidden='true'></span> Remove</button></td>
               </tr>";
             }
           }
         }
    
        $data .="</tbody></table>
          <hr>
          <a href='checkout.php'> <h3 align='right'>Total: &#x20B1; <span 
              id='grandTotal'>$grand_total </span><br><button class='btn btn- 
              success btn-lg'><span class='glyphicon glyphicon-ok-sign'></span> 
              Check Out</button></h3>  </a>
              <hr>";
    
       echo $data;
    ?>
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Manav VVV    6 年前

    你的代码没有检查 $_SESSION['item_count'] 是0。它正在获取 $_SESSION['cart'] .你需要检查一下 $会话['项目\计数'] 是0或销毁 $课程[购物车] 例如:

    <?php session_start();
      if ($_GET['checkout'] =='success') {
      session_destroy();
     }
    ?>