Reputation: 41
My shopping cart does not function right.
If i add one item to the cart it works accordingly, but when i add a second or multiple items, the cart seems act strange...
When adding a second item, the quantity of the second item starts at 2.
The quantity of a newly added item starts with the amount of items pressent in the cart. It also increments with the amount of items in the cart.
After adding a third item, the item is displayed as many times as there are items it the cart...
How can change it to add a single item and only increment by one if the items is existing in the cart?
Thanks in advance!
<?php
require "core.inc.php";
require "connect.inc.php";
?>
<html>
<head>
<meta charset="UTF-8">
<title>Winkelwagen</title>
<link rel="stylesheet" type="text/css" href="winkelwagen.css" />
<link rel="stylesheet" type="text/css" href="topAndMenuHeader.css" />
</head>
<body bgcolor="#8A8B93">
<div id="container">
<?php include "topAndMenuHeader.php"; ?>
<div id="content">
<p class="opmaakTitel">Winkelwagen</p>
<?php
if ( isset($_GET['itemID']) ) {
$itemID = $_GET['itemID'];
$sql = "SELECT * FROM items WHERE id=$itemID";
if ( $query = mysql_query($sql) ) {
$numRows = mysql_num_rows($query);
if ( $numRows == 1 ) {
$artID = mysql_result($query, 0, 'id');
$artNaam = mysql_result($query, 0, 'artNaam');
$artPrijs = mysql_result($query, 0, 'artPrijs');
$artAfbeelding = mysql_result($query, 0, 'artAfbeelding');
$artAantal = 1;
$index = -1;
if ( isset($_SESSION['cart']) ) {
unserialize(serialize($_SESSION['cart']));
for ( $i = 0; $i < count($_SESSION['cart']); $i++ )
{
if ( $_SESSION['cart'][$i]['id'] ==
$_GET['itemID'] ) {
$index = $i;
}
if ( $index == -1 ) {
array_push($_SESSION['cart'],
array('id'=>$artID, 'naam'=>$artNaam, 'prijs'=>$artPrijs,
'afbeelding'=>$artAfbeelding, 'aantal'=>$artAantal));
} else {
$_SESSION['cart'][$index]['aantal']++;
}
}
} else {
$_SESSION['cart'] []=array('id'=>$artID,
'naam'=>$artNaam,'prijs'=>$artPrijs,
'afbeelding'=>$artAfbeelding,'aantal'=>$artAantal);
}
}
}
}
foreach ( $_SESSION['cart'] as $cart ) {
echo '<div id="artikelSpace">';
echo '<div id="artikel">';
echo '<div id="afbeelding">';
echo'<imgsrc="data:image/jpeg;base64,'
.base64_encode($cart['afbeelding']).'" />';
echo '</div>';
echo '<div id="naam">';
echo '<p>'.$cart['naam'].'</p>';
echo '</div>';
echo '<div id="aantal">';
echo '<form action="winkelwagen.php" method="POST">';
echo 'Aantal:';
echo '<input type="text" name="aantal" value="'
.$cart['aantal'].'" style="width: 50px; margin: 0px 20px 0px 20px" />';
echo '<input type="submit" name="wijzigAantal"
value="Wijzig" style="width: 100px;
border-radius: 5px; font-weight: bold;" />';
echo '</form>';
echo '</div>';
echo '<div id="prijs">';
echo '<p>Prijs: €'.($cart['prijs'] * $cart['aantal']).'</p>';
echo '</div>';
echo '<div id="verwijder">';
echo '<ul>';
echo '<li><a href="#">Verwijder</a></li>';
echo '</ul>';
echo '</div>';
echo '</div>';
echo '</div>';
}
?>
</div>
</div>
</body>
</html>
Upvotes: 2
Views: 85
Reputation: 16963
You have to initialize the $index
variable in each iteration of the loop, like this:
// your code
if ( isset($_SESSION['cart']) ) {
unserialize(serialize($_SESSION['cart']));
for ( $i = 0; $i < count($_SESSION['cart']); $i++ ) {
$index = -1; // initialize $index in each iteration
if ( $_SESSION['cart'][$i]['id'] == $_GET['itemID'] ) {
$index = $i;
}
if ( $index == -1 ) {
array_push($_SESSION['cart'], array('id'=>$artID, 'naam'=>$artNaam, 'prijs'=>$artPrijs, 'afbeelding'=>$artAfbeelding, 'aantal'=>$artAantal));
} else {
$_SESSION['cart'][$index]['aantal']++;
}
}
} else {
$_SESSION['cart'][]=array('id'=>$artID, 'naam'=>$artNaam,'prijs'=>$artPrijs, 'afbeelding'=>$artAfbeelding,'aantal'=>$artAantal);
}
// your code
Sidenote: Don't use mysql_
database extensions, they were deprecated in PHP 5.5.0 and were removed in PHP 7.0.0. Use mysqli
or PDO
extensions instead. And this is why you shouldn't use mysql_
functions.
Upvotes: 1