php - 如何在另一个 PHP 页面中显示信息

标签 php html mysql sql

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

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

元素名称、元素型号、元素价格

                 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;
}
?>

最佳答案

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

关于php - 如何在另一个 PHP 页面中显示信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13778747/

相关文章:

javascript - jquery删除除了点击的那个之外的所有div

php - 类似于 Facebook 的 Mysql 结构

html - 外部容器(围绕 div 元素的图像)

html - 将 <li> 中的文本基线向上移动 2 px

php - MySQL 字符集应该与我的 php 文件字符集相同吗?

php - 查询 MySQL 表中的多列 (PHP)

包含 PHP 文件但代码不起作用

php - 使用 mysqli_num 将数据库列的所有值存储在数组中(为什么不起作用)

php - 你能查询一个数组吗?拉维尔 5.2

html - 如何使滚动条仅适用于选项卡,而不适用于内容?