【问题标题】:Creating a PHP shopping cart using sessions from an array使用数组中的会话创建 PHP 购物车
【发布时间】:2016-02-04 08:46:33
【问题描述】:

好的,所以我有以下页面,这将是我的购物车页面,我无法使用会话从 1 页上的产品数组中获取值并创建一个正常运行的购物车。我不清楚如何使用 php 会话变量来维护诸如购物车之类的持久信息。我想弄清楚如何使用 $_SESSION['cart'] 创建购物车以及如何将数据传递到其中,以便我可以显示购物车中每个项目的数量并具有添加/删除功能。

<!DOCTYPE html>
<html lang="en">

  <?php include 'header.php';

    if(!empty($_SESSION['cart'])){

    }
  ?>


    <title>Football Items</title>
    <b><i><h1>Shopping Cart - Checkout</h1><b></i>
    <!-- Bootstrap -->

   <link rel="stylesheet" href="styles/styles1.css">

<link rel="stylesheet" href="css/bootstrap.min.css">

  </head>

  <!--NavStart-->
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="navbar-collapse-1" aria-expanded="false">
      </button>
     <b><a class="navbar-brand" href="index.php">Home Page</a></b>
    </div>


    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a href="">About Us <span class="sr-only">(current)</span></a></li>


        <li><a href="productlist.php">Football Jerseys</a></li>
        <li class="dropdown">

      <ul class="nav navbar-nav navbar-right">
        <li><a href="Application.php">Checkout</a></li>
        <li class="dropdown">

          </ul>
        </li>
      </ul>
    </div>
  </div>
</nav>



  <div id="wrapper">
  <div id="wrap">


  <?php
      if (!empty($_GET['act'])) 
      {
        if ($_GET['act']=='add') {
            $numr = $_GET['itemid'];
            $name = $_GET['itemname'];
            $quantity = 1;
            $price = $_GET['itemprice'];
            $total = $quantity * $price;
        }
      }
  ?>



  <body>



  <table border="3" style="height: 100% width:200% cellpadding="3" cellspacing="3">
    <tr>
     <th>Item Number</th></p>
      <th>Item Name</th>
      <th>Price</th>
      <th> Quantity </th>
      <th>Total</th>
    </tr>


      <?php
                echo "<tr>";
                echo "<th>$numr</th>";
                echo "<td>$name</td>";
                echo "<td>$price</td>";
                echo "<td>$quantity</td>";
                echo "<td>$total</td>";
                echo "</tr>";

            ?>




  </table>
  </div>
</div>

  </body>

     <div class="container"> 
     <button type="button" style= color:black;background-color:white;" class="btn btn-primary active">
        <a href="form.php">Checkout now.</a></button>

  </div>
</div>


<?php include 'footer.php';?>  

【问题讨论】:

    标签: php session-variables shopping-cart


    【解决方案1】:

    在您初始化会话的产品页面上,确保您的 html 标记上方有 session_start()

    然后你需要给会话标签一个值,比如$_SESSION["cart"] = "";

    您还需要将所有内容添加到 1 个数组中,以便稍后在购物车页面上查看该数组。

    为此使用array_push,在第一次创建会话时创建一个数组。然后使用 array_push 将商品添加到购物车。

    <?php
    $a=array("red","green");
    array_push($a,"blue","yellow");
    print_r($a);
    ?>
    

    输出:Red, Green, Blue, Yellow

    使用它来将产品添加到数组中。在您的购物车页面上,您只需简单地计算数组中有多少商品。循环将它们全部取出,您就完成了!

    【讨论】:

    • array_push 是否追加?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2016-01-29
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    相关资源
    最近更新 更多