gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:49:17 25 4
gpt4 key购买 nike

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

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

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

                 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com