Ik ben bezig met het maken van een eigen webshop voor school. Ik loop echter op dit moment tegen twee problemen aan:
Probleem 1:
Na een hoop gepiel is het me gelukt om het aantal van een product in de cart aan te passen en dit aantal door te voeren in de cart_array session. Maar er treedt een probleem op als ik de aantallen van verschillende producten tegelijk wil aanpassen, want dan pakt hij het nieuwe ingevulde aantal en voert die door op alle producten. Dus als ik bijv. product 1 aantal aanpas naar 5, past hij het aantal in de session van product 2,3,4 etc. ook aan naar 5. Ik weet dat de fout in het pakken van het nieuwe ingevulde aantal zit, want hij pakt dus maar één aantal en voert die door voor alle producten, maar ik weet niet hoe ik het kan oplossen.
Probleem 2:
Mijn tweede probleem komt bij het verwijderen van de verschillende producten uit de winkelwagen. Het verwijderen gaat goed, het lukt om er één te verwijderen, maar ook meerdere tegelijk. Echter als je bijv. het eerste product verwijdert uit de $_SESSION['cart_array'] en je hebt bijv. nog 3 andere producten erin staan, worden de indexen: 1-2-3, terwijl het eerst 0-1-2-3 was. En dan kloppen mijn checkboxen niet meer, want als je dan weer het eerste product wil verwijderen, wil hij de 0-index pakken, maar die is er dus niet meer. Ik heb de regel sort($_SESSION['cart_array']; onder de regel unset($_SESSION['cart_array'][''.$remove_id.'']); proberen te plakken, maar dan verwijdert hij gewoon alle indexen. Hoe kan dit?
Dit is de code:
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 1 (if user attempts to add something to the cart from the product page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($_POST['pid']))
{
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION['cart_array']) || count($_SESSION['cart_array']) < 1) {
// RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION['cart_array'] = array(0 => array('product_id' => $pid, 'qty' => 1));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION['cart_array'] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "product_id" && $value == $pid) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("product_id" => $pid, "qty" => $each_item['qty'] + 1)));
$wasFound = true;
} // close if condition
} // close while loop
} // close foreach loop
if ($wasFound == false) {
array_push($_SESSION["cart_array"], array("product_id" => $pid, "qty" => 1));
}
}
header("location: cart.php");
exit();
}
?>
<form action="" method="post" enctype="multipart/formdata">
<table align="center" width="700" bgcolor="skyblue">
<tr align="center">
<th>Remove</th>
<th>Product(s)</th>
<th>Quantity</th>
<th>Total Price</th>
</tr>
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 4 (render the cart for the user to view)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = '';
$cartTotal = '';
if (!isset($_SESSION['cart_array']) || count($_SESSION['cart_array']) < 1) {
echo $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
$i = 0;
foreach ($_SESSION['cart_array'] as $each_item) {
$product_id = $each_item['product_id'];
$sql = $db->prepare("SELECT * FROM products WHERE product_id = '$product_id' LIMIT 1");
$sql->execute();
while ($row = $sql->fetch()) {
$product_price = $row['product_price'];
$product_title = $row['product_title'];
$product_image = $row['product_image'];
}
$price_total = $product_price * $each_item['qty'];
$cartTotal = $price_total + $cartTotal;
$i++;
// Items verwijderen
if(isset($_POST["remove_cart"]))
{
// Access the array and run code to remove that array index
foreach($_POST['remove_item'] as $remove_id)
{
unset($_SESSION['cart_array'][''.$remove_id.'']);
}
header('location: cart.php');
}
// Itemaantallen aanpassen
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
$item_to_adjust = $_POST['item_to_adjust'];
$quantity = $_POST['qty'];
$i = 0;
foreach($_POST['item_to_adjust'] as $adjust_id)
{
$i++;
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("product_id" => $adjust_id, "qty" => $quantity)));
}
}
?>
<tr align="center">
<td>
<td><input type="checkbox" name="remove_item[]" value="<?php echo $i - 1;?>" /></td>
<td><?php echo $product_title; ?><br />
<img src="admin_area/product_images/<?php echo $product_image;?>" width="60" height="60" /> </td>
<input name="item_to_adjust[]" type="hidden" value="<?php echo $each_item['product_id'];?>" />
<td><input type="text" size="4" name="qty" value="<?php echo $each_item['qty'];?>" maxlength="1" /></td>
<td><?php echo '€'.$price_total; ?></td>
</tr>
<?php
}
}
?>
<tr align="right">
<td colspan="4"><b>Sub Total:</b></td>
<td colspan="4"><?php echo '€'.$cartTotal; ?></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="remove_cart" value="Remove Item(s)" /></td>
<td></td><input name="adjustBtn" type="submit" value="Update" /></td>
<td><input type="submit" name="continue" value="Continue shopping" /></td>
<td><a href="checkout.php"><button>Checkout</button></a></td>
<td><a href="cart.php?cmd=emptycart">Empty Cart!</a></td>
</tr>
</table>
</form>Ik kom er maar niet uit!
Alvast bedankt, Mike