我在使用迷你购物车时遇到了麻烦。我创建了迷你购物车,当添加产品时,它会进入购物篮页面。但是,我想创建一个结帐页面。我如何抓住购物篮中的产品并将其放入结帐页面。
因此,例如,结帐页面将如下所示
项目名称,项目模型,项目价格
TOTAL OF ITEMS
CHECKOUT BUTTON(链接到支付系统)
这是我的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 :(得分:2)
只要在每个页面上都有session_start();
,您就应该能够使用与此编码完全相同的foreach循环。与信息的会话将被转移到下一页。
答案 1 :(得分:2)
假设您有一个名为test.php的页面,其中包含以下代码:
<?php
session_start();
$_SESSION['test'] = 1;
echo $_SESSION['test'];
?>
然后要访问othertest.php页面中的测试变量,只需执行以下操作:
<?php
session_start();
echo $_SESSION['test']; ?>
答案 2 :(得分:0)
您忘记将session_start()
添加到php文件的顶部。我建议将session_start()
添加到单独的文件中,然后将该文件包含在使用会话变量的每个php页面中。