Producten
<?php
session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
case "add":
if(!empty($_POST["aantal"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM tblproduct WHERE id='" . $_GET["id"] . "'");
$itemArray = array($productByCode[0]["id"]=>array('naam'=>$productByCode[0]["naam"], 'id'=>$productByCode[0]["id"], 'aantal'=>$_POST["aantal"], 'prijs'=>$productByCode[0]["prijs"], 'image'=>$productByCode[0]["image"]));
if(!empty($_SESSION["item"])) {
if(in_array($productByCode[0]["id"],array_keys($_SESSION["item"]))) {
foreach($_SESSION["item"] as $k => $v) {
if($productByCode[0]["id"] == $k) {
if(empty($_SESSION["item"][$k]["aantal"])) {
$_SESSION["item"][$k]["aantal"] = 0;
}
$_SESSION["item"][$k]["aantal"] += $_POST["aantal"];
}
}
} else {
$_SESSION["item"] = array_merge($_SESSION["item"],$itemArray);
}
} else {
$_SESSION["item"] = $itemArray;
}
}
break;
case "remove":
if(!empty($_SESSION["item"])) {
foreach($_SESSION["item"] as $k => $v) {
if($_GET["id"] == $k)
unset($_SESSION["item"][$k]);
if(empty($_SESSION["item"]))
unset($_SESSION["item"]);
}
}
break;
case "empty":
unset($_SESSION["item"]);
break;
}
}
?>
<HTML>
<HEAD>
<TITLE>Winkelmand</TITLE>
<link href="style.css" type="text/css" rel="stylesheet" />
</HEAD>
<BODY>
<div id="shopping-cart">
<div class="txt-heading">Winkelmand</div>
<a id="btnEmpty" href="index.php?action=empty">Winkelmand leegmaken</a>
<?php
if(isset($_SESSION["item"])){
$total_aantal = 0;
$total_prijs = 0;
?>
<form method="post" action="bestelling.php">
<table class="tbl-cart" cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th style="text-align:left;">Naam</th>
<th style="text-align:left;">ID</th>
<th style="text-align:right;" width="5%">Aantal</th>
<th style="text-align:right;" width="10%">Stuksprijs</th>
<th style="text-align:right;" width="10%">Prijs</th>
<th style="text-align:center;" width="5%">Verwijderen</th>
</tr>
<?php
foreach ($_SESSION["item"] as $item){
$item_prijs = $item["aantal"]*$item["prijs"];
?>
<tr>
<td><img src="<?php echo $item["image"]; ?>" class="cart-item-image" /><?php echo $item["naam"]; ?></td>
<td><?php echo $item["id"]; ?></td>
<td style="text-align:right;"><?php echo $item["aantal"]; ?></td>
<td style="text-align:right;"><?php echo "€ ".$item["prijs"]; ?></td>
<td style="text-align:right;"><?php echo "€ ". number_format($item_prijs,2); ?></td>
<td style="text-align:center;"><a href="index.php?action=remove&id=<?php echo $item["id"]; ?>" class="btnRemoveAction"><img src="icon-delete.png" alt="Remove Item" /></a></td>
</tr>
<?php
$total_aantal += $item["aantal"];
$total_prijs += ($item["prijs"]*$item["aantal"]);
}
?>
<tr>
<td colspan="2" align="right">Totaal:</td>
<td align="right"><?php echo $total_aantal; ?></td>
<td align="right" colspan="2"><strong><?php echo "€ ".number_format($total_prijs, 2); ?></strong></td>
<td></td>
</tr>
</tbody>
</table>
<input type="submit" value="Bestellen">
</form>
<?php
} else {
?>
<div class="no-records">Winkelmand is leeg</div>
<?php
}
?>
</div>
<div id="product-grid">
<div class="txt-heading">Producten</div>
<?php
$product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
if (!empty($product_array)) {
foreach($product_array as $key=>$value){
?>
<div class="product-item">
<form method="post" action="index.php?action=add&id=<?php echo $product_array[$key]["id"]; ?>">
<div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>"></div>
<div class="product-tile-footer">
<div class="product-title"><?php echo $product_array[$key]["naam"]; ?></div>
<div class="product-prijs"><?php echo "€".$product_array[$key]["prijs"]; ?></div>
<div class="cart-action"><input type="text" class="product-aantal" name="aantal" value="1" size="2" /><input type="submit" value="Winkelmand" class="btnAddAction" /></div>
</div>
</form>
</div>
<?php
}
}
// code sessie wissen:
/*
session_start();
session_destroy();
header('Location: index.php');
*/
show_source(__FILE__);
?>
</div>
<a href="dbcontroller_voorbeeld.php">DBcontroller<a></br>
<a href="tblproduct.sql">SQL-code<a></br>
<a href="downloadwinkelmand.zip">Download winkelmand<a>
</BODY>
</HTML>
DBcontroller
SQL-code
Download winkelmand