2015-11-04 103 views
0

好吧,所以我有以下頁面將成爲我的購物車頁面,並且我無法使用會話從1頁上的產品數組中獲取值並創建一個功能大車。我不清楚如何使用php會話變量來保持持久性信息,例如購物車。我想知道如何使用$ _SESSION ['cart']創建購物車以及如何將數據傳遞給它,以便我可以顯示購物車中每件物品的數量並具有添加/刪除功能。使用數組中的會話創建一個PHP購物車

<!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';?> 

回答

1

在你的產品頁面初始化會話,請確保您有session_start()你的HTML標記上面。

然後,你需要給會話變量的值,如$_SESSION["cart"] = "";

您還需要一些東西來在1個陣列添加一切,所以您可以稍後檢查數組您的購物車頁面上。

爲此使用array_push,首次創建會話時創建一個數組。然後使用array_push將項目添加到購物車。

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

輸出:Red, Green, Blue, Yellow

使用此產品添加到陣列中。在您的購物車頁面上,您只需計算陣列中有多少物品即可。製作一個循環讓他們全部出來,你就完成了!

+0

array_push append? –

+0

@MaalB是的! – paradizzee