AYUDA URGENTE con array y bucle de php
Publicado por jose (2 intervenciones) el 22/10/2017 03:29:36
poncarrito.php
tienda.php
carito.js
EL PROBLEMA ESTA EN PONCARRITO.PHP YA QUE CUANDO AGREGO UN PRODUCTO Y SIGO AGREGANDO LA CANTIDAD DE ESTE A LA 3RA PUESTA SALE SIN NADA Y ASI A LA QUINTA A LA SEPTIMA A RATOS PARECE QUE VALE PERO SALE VACIO ALGO ASI Y NO SE PORQUE ES O COMO ARREGLAR

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
session_start();
if(isset($_GET['p'])){
$_SESSION['producto'][$_SESSION['contador']] = $_GET['p'];
$_SESSION['contador']++;
}
if (isset($_GET['c'])){
$_SESSION['cantidad'][$_SESSION['contador']] = $_GET['c'];
}
include '../conexion/server.php';
$suma = 0;
for ($i=0; $i < $_SESSION['contador']; $i++){
//echo "producto:".$_SESSION['producto'][$i]."-".$_GET['c'][$i]."<br>";
$cab = $_SESSION['cantidad'][$i+1];
//var_dump($cab);
//die();
$query = mysqli_query($conect,'SELECT * FROM Productos WHERE id='.$_SESSION['producto'][$i].'');
//var_dump($_SESSION['producto'][$i]);
//die();
while ($fila = mysqli_fetch_array($query)) {
$total = $fila['Precio']*$cab;
echo '<tr>';
echo '<td>'.$fila['Nombre'].'</td>';
echo '<center><td>';
echo '<center><input type="number" name="ja" class="la" value="'.$cab.'""></center>';
echo '</td></center>';
echo '<td>'.number_format($total,2).'</td>';
echo '<td>';
echo $i;
//echo '<input type=hidden name=ya class=na value='.$i.'>';
//echo '<form action="functions/poncarrito.php" method="post">';
//echo '<button type=submit name="ka" class="boton_borrar">borrar</button>';
//echo '</form>';
echo '<a href=functions/poncarrito.php?a='.$i.'>X</a>';
echo '</td>';
echo '</tr>';
$suma += $total;
}
}
if(isset($_GET['a'])){
echo '<script>window.location="../tienda.php";</script>';
unset($_SESSION['producto'][$_GET['a']]);
unset($_SESSION['cantidad'][$_GET['a']+1]);
$_SESSION['producto'] = array_values($_SESSION['producto']);
$_SESSION['cantidad'] = array_values($_SESSION['cantidad']);
$_SESSION['contador']--;
}
echo '<tr><td></td><td>Subtotal</td><td>$'.number_format($suma,2).'</td></tr>';
mysqli_close($conect);
?>
tienda.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
session_start();
if(!isset($_SESSION['contador'])){
$_SESSION['contador'] = 0;
}
?>
echo ' <h3><a href="detail.html">'.$nombre.'</a></h3>';
echo '<p class="price">$'.$precio.'</p>';
echo '<p class="buttons">';
echo '<a href="detail.html" class="btn btn-default">Ver Detalle</a>';
echo '<input type="number" name="ja" value="0" id="jaa" class="la ma">';
echo '<button value="'.$id.'" class="boton_compra">Añadir Carrito</button>';
carito.js
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
$(document).ready(inicio)
function inicio(){
$(".boton_compra").click(anade);
$("#carrito").load("functions/poncarrito.php");
$("#carrito2").load("functions/checkout.php");
$("#pa").click(actualiza);
}
function anade(){
var fila = $(this).parents("p");
if(fila.find(".ma").val() >0){
$("#carrito").load("functions/poncarrito.php?p="+$(this).val());
$("#carrito").load("functions/poncarrito.php?c="+fila.find(".ma").val());
$("#carrito2").load("functions/checkout.php?p="+$(this).val());
$("#carrito2").load("functions/checkout.php?c="+fila.find(".ma").val());
}else{
alert("Ingrese cantidad");
}
}
EL PROBLEMA ESTA EN PONCARRITO.PHP YA QUE CUANDO AGREGO UN PRODUCTO Y SIGO AGREGANDO LA CANTIDAD DE ESTE A LA 3RA PUESTA SALE SIN NADA Y ASI A LA QUINTA A LA SEPTIMA A RATOS PARECE QUE VALE PERO SALE VACIO ALGO ASI Y NO SE PORQUE ES O COMO ARREGLAR

Valora esta pregunta


0