I am creating a webshop with PHP and use sessions to save temp items.
<form method="post" action="basket.php">
<table border="1" style="width:400px;">
<tr>
<td style="width:35%;">Product</td>
<td style="width:30%;">Aantal</td>
<td style="width:25%;">Totale prijs</td>
<td style="width:10%;"></td>
</tr>
<?php
for($i = 0; $i < count($_SESSION['basket']); $i++) {
$id = $_SESSION['basket'][$i]['itemId'];
$number = $_SESSION['basket'][$i]['number'];
$result = $mysqli->query('SELECT * FROM items WHERE id = "' . $id . '"');
$row = $result->fetch_assoc();
$total = $number * $row['price'];
?>
<tr>
<td>
<a href="view_item.php?id=<?php echo $id; ?>">
<?php echo $row['name']; ?>
</a>
</td>
<td><?php echo $number; ?></td>
<td>€ <?php echo $total; ?></td>
<td><input type="submit" name="removeItem[]" value="Verwijder" /></td>
</tr>
<?php
if (isset($_POST['removeItem'])) {
unset($_SESSION['basket'][$i]);
}
$_SESSION['basket'] = array_values($_SESSION['basket']);
}
?>
</table>
</form>
When i set 3 items into the session.
Array
(
[basket] => Array
(
[0] => Array
(
[itemId] => 1
[number] => 1
[timestamp] => 1380722942
)
[1] => Array
(
[itemId] => 1
[number] => 1
[timestamp] => 1380722944
)
[2] => Array
(
[itemId] => 1
[number] => 1
[timestamp] => 1380722945
)
)
)
And remove the second item, it removes the last item also..
What goes wrong?
Can somebody help me?