Hi,
I am busy creating a shopping cart and this seems to work fine.
<?php
session_start();
//CHECK IF THE DELETE BUTTON IS PRESSED
if (isset($_GET['d'])) {
$id = $_GET['d'];
unset($_SESSION['cart'][$id]);
echo 'deleted : ' . $id;
}
if ($_POST['subReq']) {
$_SESSION['cart'][] = array(
'artist'=>$_POST['fld_artist'],
'cards'=>$_POST['fld_cards']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Winkel - TEST</title>
</head>
<body>
<?php
echo 'Array counts = ' . count($_SESSION['cart']);
echo '<br />';
?>
<form action="" method="post">
<table width="566" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="166">Artist :</td>
<td width="400">
<select name="fld_artist" id="fld_artist" class="input200px">
<option value="Jannes">Jannes</value>
<option value="Hazes">Andre Hazes</value>
<option value="MooiWark">Mooi Wark</value>
</select>
</td>
</tr>
<tr>
<td width="166">Cards :</td>
<td width="400">
<input name="fld_cards" id="fld_cards" type="text" class="input200px" maxlength="25" />
</td>
</tr>
<tr>
<td width="166">
</td>
<td width="400">
<input type="submit" id="subReq" name="subReq" class="style4" value="Aanvraag" />
</td>
</tr>
</table>
</form>
<form action="" method="post">
<?php
echo '<table width="566" border="0" cellpadding="0" cellspacing="0">';
for ($cnt = 0; $cnt < count($_SESSION['cart']); $cnt++) {
echo '<tr>';
echo '<td width="300">' . $_SESSION['cart'][$cnt]['artist'] . '</td>';
echo '<td width="150">' . $_SESSION['cart'][$cnt]['cards'] . '</td>';
echo '<td width="116">
<a href="index1.php?d=' . $cnt . '" target="_self"><img src="iconno.png" /></a>
</td>';
echo '</tr>';
}
echo '</table>';
echo '<br /><br />';
print_r($_SESSION['cart']);
?>
</form>
</body>
</html>
I can add things and it will display nice in a table.
However now I want to delete a item from the array and that's not working correct
When I unset using the ID from $_SESSION['cart'] it will remove the item from array, however the output is also messed up.
Example
Jannes, 2 cards
Hazes, 3 cards
MooiWark, 4 cards
Array ( [0] => Array ( [artist] => Jannes [cards] => 2 ) [1] => Array ( [artist] => Hazes [cards] => 3 ) [2] => Array ( [artist] => MooiWark [cards] => 4 ) )
When I delete Hazes, the table also doesn't display MooiWark anymore.
Array after delete is
Array ( [0] => Array ( [artist] => Jannes [cards] => 2 ) [2] => Array ( [artist] => MooiWark [cards] => 4 ) )
This is because I loop thru the array with count (0 - 1), but the MooiWark ID = 2.
How can I resort the array so that Mooiwark will become ID=1 ?