No borra registros del Array (Para carrito de compras)
Publicado por Puchenko (2 intervenciones) el 30/11/2019 00:16:38
Estoy implementando un carrito de compras, y en la pagina donde el usuario ve todo su carrito con los productos agregados, le agregue la opción "Quitar" o eliminar a cada producto (son libros) , lo que hice fue enganchar cada boton de quitar con el indice correspondiente de su elemento-fila en el array bidimensional. El problema es que solo me deja eliminar el primer elemento(primer fila) y el resto no. Si me fijo en el inspector de elementos en el href de Quitar, los indices son correctos.
Que estoy haciendo mal?
Que estoy haciendo mal?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<tbody class="tbody">
<?php
$total = 0;
//SESSION cart Trae los productos que el usuario agrego al carrito en un array
$array = $_SESSION['cart'];
if (isset($_REQUEST['takeOff'])) {
$takeKey = $_REQUEST['takeOff'];
array_splice($array,$takeKey,1);
}
for($i = 0; $i < count($array); $i++){
$id = $array[$i]['id'];
$name = $array[$i]['name'];
$author = $array[$i]['author'];
$img = $array[$i]['img'];
$price = $array[$i]['price'];
$quantity = $array[$i]['quantity'];
$stock = $array[$i]['stock'];
$total += $price;
?>
<tr>
<td class="align-middle" scope="row"><b><?php echo $name; ?></b></td>
<td class="align-middle" ><?php echo $author; ?></td>
<td>
<div class="text-center d-inline-block mx-auto img-carrito">
<img class="img-fluid" src="<?php echo $img; ?>" alt="<?php echo $name; ?>"
title="<?php echo $name; ?>"/>
</div>
</td>
<td class="align-middle">
<form name="formulario1" oninput="price.value = parseInt(quantity.value) * parseInt(unitPrice.value)">
<input name="quantity" type="number" min="0" max="<?php echo $stock; ?>" value="<?php echo $quantity; ?>">
<input name="unitPrice" type="hidden" value="<?php echo $price; ?>">
</td>
<td class="align-middle">
<p class="mb-1 lead">
$ 
<output name="price" for="quantity unitPrice" value="<?php echo $price; ?>"><?php echo $price; ?></output>
</p>
</form>
<?php echo "<a href='./cart.php?takeOff=" . key($array) . "' title='Quitar del Carrito'>";?>
<?php next($array); ?>
<button class="btn btn-sm btn-dark take-off">Quitar</button>
</a>
</td>
</tr>
<?php } ?>
<tr>
<td></td><td></td><td></td>
<td>
<button onclick=window.location="buybook.php">Comprar</button>
</td>
<td>
<h5 class="font-weight-bold mt-2">Total: <?php echo $total; ?></h5>
</td>
</tr>
</tbody>
Valora esta pregunta


0