2012-12-08 92 views
1

我遇到小型購物車時遇到問題。我創建了迷你購物車,當添加產品時,它會進入購物籃頁面。不過,我想創建一個結帳頁面。我如何抓取籃子裏的產品並將其放入結帳頁面。如何在另一個PHP頁面中顯示信息

所以例如,檢查頁面看起來就像這樣

物品名稱,產品型號,商品價格

    TOTAL OF ITEMS 

Checkout按鈕(鏈接到一個支付系統)

這是我的Basket.php代碼:

<?php 
    $bikecode = $_GET['id'];  //the product id from the URL 
    $action = $_GET['action']; //the action from the URL 

if($bikecode && !productExists($bikecode)) { 
    die("Product Doesn't Exist"); 
} 

    switch($action) { //decide what to do 

     case "add": 
      $_SESSION['cart'][$bikecode]++; //add one to the quantity of the product with id $bikecode 
     break; 

     case "remove": 
      $_SESSION['cart'][$bikecode]--; //remove one from the quantity of the product with id $bikecode 
      if($_SESSION['cart'][$bikecode] == 0) unset($_SESSION['cart'][$bikecode]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. 
     break; 

     case "empty": 
      unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
     break; 
    } 

    if($_SESSION['cart']){ 

     echo "<table width=\"100%\">"; 

      foreach($_SESSION['cart'] as $bikecode => $quantity) { 
      $sql = sprintf("SELECT BikeCode, Model, Price FROM Bike WHERE BikeCode = '%s';", $bikecode); 

       $result = mysqli_query($con, $sql); 

       if(mysqli_num_rows($result) > 0) { 

        list($bikecode, $model, $price) = mysqli_fetch_row($result);  

        $cost = $quantity * $price; 
        $total = $total + $cost; 

         echo "<tr><th>BikeCode:</th><th>Model:</th><th>Quantity:</th><th>Price:</th></tr>"; 
         echo "<tr>"; 
         echo "<td align=\"center\">$bikecode</td>"; 
         echo "<td align=\"center\">$model</td>"; 
         echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$bikecode\">X</a></td>"; 
         echo "<td align=\"center\">£$cost</td>"; 
        echo "</tr>"; 
       } 
      } 

      echo "<tr>"; 
       echo "<td colspan=\"3\" align=\"right\">Total</td>"; 
       echo "<td align=\"right\">£$total</td>"; 
      echo "</tr>"; 

      echo "<tr>"; 
       echo "<td colspan=\"4\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>"; 
      echo "</tr>";  
     echo "</table>"; 

    }else{ 
     echo "You have no items in your shopping cart."; 
    } 

function productExists($bikecode) { 
    $sql = sprintf("SELECT * FROM Bike WHERE BikeCode = '%s';", $bikecode); 
    return mysqli_num_rows(mysqli_query($con, $sql)) > 0; 
} 
?> 
+0

如果您已經在使用cookies,爲什麼不只是將這些變量回顯到您的結帳頁面? – patrickdamery

+0

我試過不知道該怎麼做。 – Hii

回答

2

只要你有一個session_start(); on你應該能夠使用與你在這段代碼中完全一樣的foreach循環。有關信息的會議將轉入下一頁。

2

假設你有一個名爲test.php的用下面的代碼頁:

<?php 
session_start(); 
$_SESSION['test'] = 1; 
echo $_SESSION['test']; 
?> 

然後訪問測試變量othertest.php頁面,只需做到以下幾點:

<?php 
session_start(); 
echo $_SESSION['test']; ?> 
0

你忘了將session_start()添加到您的PHP文件的頂部。我建議將session_start()添加到單獨的文件中,然後在使用會話變量的每個php頁面中包含該文件。

相關問題