1

我在使用迷你购物车时遇到问题。我已经创建了迷你购物车,当添加产品时,它会进入购物篮页面。但是,我想创建一个结帐页面。我如何抓住购物篮中的产品并将其放入结帐页面。

例如,结帐页面将如下所示

商品名称、商品型号、商品价格

                 TOTAL OF ITEMS

结帐按钮(链接到支付系统)

这是我的 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;
}
?>
4

3 回答 3

2

只要您session_start();在每个页面的顶部都有一个,您就应该能够使用与在这段编码中完全相同的 foreach 循环。与信息的会话将被转移到下一页。

于 2012-12-08T15:21:45.897 回答
2

假设您有一个名为 test.php 的页面,其中包含以下代码:

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

然后访问 othertest.php 页面中的测试变量,只需执行以下操作:

<?php
session_start();
echo $_SESSION['test']; ?>
于 2012-12-08T15:27:15.833 回答
0

您忘记添加session_start()到 php 文件的顶部。我建议添加session_start()到一个单独的文件,然后将该文件包含在每个使用会话变量的 php 页面中。

于 2012-12-08T15:49:50.117 回答