这是代码的php部分
<?php
if(!isset($_SESSION["userId"])){
echo "<div class='loginCart'><p>Du er ikke logget inn</p>";
include "login.php";
echo "</div>";
goto end;
}
if (isset($_POST["addToCart"])){
if (isset($_SESSION["cart"])){
$product_array_id = array_column($_SESSION["cart"],"productId");
if (!in_array($_GET["id"],$product_array_id)){
$count = count($_SESSION["cart"]);
$product_array = array(
'productId' => $_GET["id"],
'productName' => $_POST["hiddenName"],
'productPrice' => $_POST["hiddenPrice"],
'productQuantity' => $_POST["productQuantity"],
);
$_SESSION["cart"][$count] = $product_array;
echo '<script>window.location="cart.php"</script>';
}else{
echo '<script>window.location="cart.php"</script>';
}
}else{
$product_array = array(
'productId' => $_GET["id"],
'productName' => $_POST["hiddenName"],
'productPrice' => $_POST["hiddenPrice"],
'productQuantity' => $_POST["productQuantity"],
);
$_SESSION["cart"][0] = $product_array;
}
}
if (isset($_GET["action"])){
if ($_GET["action"] == "remove"){
foreach ($_SESSION["cart"] as $keys => $value){
if ($value["productId"] == $_GET["id"]){
unset($_SESSION["cart"][$keys]);
echo '<script>window.location="cart.php"</script>';
}
}
}
}
?>
<?php
if(!empty($_SESSION["cart"])){
$totalPrice = 0;
foreach ($_SESSION["cart"] as $key => $value) {
?>
这是html申请表格
<table class='productTable' style=" margin-top: ; width: 100%; background-color:white; margin:auto; border: solid 2px black;">
<tr>
<th width="30%">Produkt navn</th>
<th width="10%">Antall</th>
<th width="13%">Pris per</th>
<th width="10%">Total Pris</th>
<th width="17%">Fjern produkt</th>
</tr>
<tr>
<td align="center"><?php echo $value["productName"]; ?></td>
<td align="center">
<form method="POST" class="checkoutForm">
<input type="number" name="quantity" value="<?php echo $value["productQuantity"];?>" min="0" style="text-align:center; margin: auto; width: 25%;"></td>
<td align="center"><?php echo number_format($value["productPrice"]);?>-,NOK</td>
<td align="center"><?php echo number_format($value["productQuantity"] * $value["productPrice"], 2);?>-,NOK</td>
<td align="center"><a href='cart.php?action=remove&id=<?php echo $value["productId"]; ?>'>Fjern</a></td>
</tr>
<?php
$totalPrice = $totalPrice + ($value["productQuantity"] * $value["productPrice"]);
}
?>
<tr>
<td></td>
<td></td>
<td></td>
<td align="center"><p style="margin: 0;">Totalpris:</p></td>
<td align="center"><strong><?php echo number_format($totalPrice, 2)."-,NOK"; ?></strong></td>
</tr>
</table>
<input class="CheckOut" type="submit" name="CheckOut" value="Check Out">
</form>