
Duda codigo
Publicado por Facundo (6 intervenciones) el 26/04/2014 19:38:08
Buenas. Tengo un problema en el codigo. Es un carrito que el usuario va ingresando un producto y su respectiva cantidad. I si ingresa un mismo producto que ya habia ingresado solo habria que aumetar la cantidad. Lo he intentado pero no me aparece como tiene que ser :/ Alguien me da un mano? Desde ya gracias
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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Punto 3</title>
</head>
<body>
<?php
session_start();
if(isset($_REQUEST["btnEnviar"])){
$bandera = false;
$producto = $_REQUEST["txtProducto"];
$cantidad = $_REQUEST["txtCantidad"];
if($bandera == false){
$_SESSION['Producto'][] = $producto;
$_SESSION['Cantidad'][] = $cantidad;
for($i=0; $i < count($_SESSION['Producto']); $i++){
echo 'Articulo: '.$_SESSION['Producto'][$i].'Cantidad: '.$_SESSION['Cantidad'][$i].'</br>';
}
$bandera = true;
}else{
for($i=0; $i < count($_SESSION['Producto']); $i++){
if($_SESSION['Producto'][$i] != $producto){
$_SESSION['Producto'][] = $producto;
$_SESSION['Cantidad'][] = $cantidad;
echo 'Articulo: '.$_SESSION['Producto'][$i].'Cantidad: '.$_SESSION['Cantidad'][$i].'</br>';
}else{
$cantidadMostrar = $_SESSION['Cantidad'][$i];
$cantidadMostrar = $cantidadMostrar + $cantidad;
echo 'Articulo: '.$_SESSION['Producto'][$i].'Cantidad: '.$cantidadMostrar.'</br>';
}
}
}
}
?>
<table>
<form>
<tr>
<th>Producto</th>
<td><input type="text" name="txtProducto" id="txtProducto"/></td>
</tr>
<tr>
<th>Cantidad</th>
<td><input type="text" name="txtCantidad" id="txtCantidad"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Añadir al Carrito" name="btnEnviar" id="btnEnviar"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Mostrar Productos" name="btnMostrar" id="btnMostrar"/></td>
</tr>
</form>
</table>
</body>
</html>
Valora esta pregunta


0