PHP - Mantener el Valor de un arreglo al hacer Post

 
Vista:
Imágen de perfil de Patricio Romero

Mantener el Valor de un arreglo al hacer Post

Publicado por Patricio Romero (2 intervenciones) el 22/07/2015 02:09:44
Estimados

Necesito mantener los valores en un array cada vez que proceso un formulario con post, o no se si habra otra forma de trabajar. la indea es a traves de un formulario agragando elementos que se ingresan a un array para mostrarse e indicar su suma total.
el codigo es el sgte.:

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
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>INGRESO DE ARTICULOS</title>
	</head>
	<body bgcolor="#D2EBF7" style="font-family: Tahoma;">
 
		<form name="form1" method="post" action="agregararticulo.php">
			<table width="282">
				<tr>
					<td width="74">Artículo</td>
					<td width="182">
						<input type="text" name="articulo" size="30" maxlength="30">
					</td>
				</tr>
				<tr>
					<td>Precio</td>
					<td>
						<input name="precio" type="text" id="precio" size="10" maxlength="5">
					</td>
				</tr>
				<tr>
					<td>Cantidad</td>
					<td>
						<input type="text" name="cantidad" size="10" maxlength="3">
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input type="submit" name="enviar" value="Enviar" id="enviar">
					</td>
				</tr>
			</table>
		</form>
 
        <?php
 
	$articulo = $_POST["articulo"];
	$rec["articulo"] = $_POST["articulo"];
	$rec["precio"] = $_POST["precio"];
	$rec["cantidad"] = $_POST["cantidad"];
	$rec["subtotal"] = $rec["precio"] * $rec["cantidad"];
	$_POST["carrito"][$articulo] = $rec;
 
?>
 
<table width='447' border='1'>
			<tr>
				<th width="188">Articulo</th>
				<th width="73">Precio</th>
				<th width="65">Cant.</th>
				<th width="93">Subtotal</th>
			</tr>
			<?php foreach ( $_POST["carrito"] as $rec ) { ?>
			<tr>
				<td><?php echo($rec["articulo"]) ?></td>
				<td><?php echo($rec["precio"]) ?></td>
				<td><?php echo($rec["cantidad"]) ?></td>
				<td><?php echo($rec["subtotal"]) ?></td>
			</tr>
			<?php } ?>
		</table>
	</body>
</html>

agradezco su ayuda

atte
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Mantener el Valor de un arreglo al hacer Post

Publicado por xve (6935 intervenciones) el 22/07/2015 09:26:43
Hola Patricio, para ello, puedes hacerlo de dos maneras... una es utilizando la base de datos que viene en los navegadores actuales, o utilizar una variable de sesión.

Aqui te adjunto tu código modificado para utilizar una variable de sesión.
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
<?php
session_start();
?>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>INGRESO DE ARTICULOS</title>
	</head>
	<body bgcolor="#D2EBF7" style="font-family: Tahoma;">
 
		<form name="form1" method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
			<table width="282">
				<tr>
					<td width="74">Artículo</td>
					<td width="182">
						<input type="text" name="articulo" size="30" maxlength="30">
					</td>
				</tr>
				<tr>
					<td>Precio</td>
					<td>
						<input name="precio" type="text" id="precio" size="10" maxlength="5">
					</td>
				</tr>
				<tr>
					<td>Cantidad</td>
					<td>
						<input type="text" name="cantidad" size="10" maxlength="3">
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input type="submit" name="enviar" value="Enviar" id="enviar">
					</td>
				</tr>
			</table>
		</form>
 
        <?php
    if($_POST["articulo"] && $_POST["precio"] && $_POST["cantidad"])
		$_SESSION["valores"][]=$_POST;
 
?>
 
<table width='447' border='1'>
	<tr>
		<th width="188">Articulo</th>
		<th width="73">Precio</th>
		<th width="65">Cant.</th>
		<th width="93">Subtotal</th>
	</tr>
	<?php foreach ( $_SESSION["valores"] as $rec ) { ?>
	<tr>
		<td><?php echo $rec["articulo"] ?></td>
		<td><?php echo $rec["precio"] ?></td>
		<td><?php echo $rec["cantidad"] ?></td>
		<td><?php echo ($rec["precio"]*$rec["cantidad"]) ?></td>
	</tr>
	<?php } ?>
</table>
</body>
</html>

espero que te sirva... coméntanos, ok?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Patricio Romero

Mantener el Valor de un arreglo al hacer Post

Publicado por Patricio Romero (2 intervenciones) el 22/07/2015 15:35:37
Estimado

Agradezco mucho la ayuda, en realidad pense que se podria realizar sin la necesidad de recurrir a las sesiones, pensaba en realizar Post persistentes como $_POST['articulo'.i$] al ir agregando junto a un contador que aumentara la variable $i.
y por curiosidad como funcionaria con esa base de datos de los navegadores, eso los desconocia.

Muchas gracias

atte.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar