PHP - Mostrar Array en una Tabla HTML

 
Vista:
Imágen de perfil de yonathan

Mostrar Array en una Tabla HTML

Publicado por yonathan (2 intervenciones) el 15/02/2017 21:54:53
Hola Amigos (as) Tengo Un Problema Con un Array, Soy Nuevo Con el Uso de Estos Array El Problema es el Siguiente Intento Crear Una Cesta de Compras Guardando el Array en Un Variable de Session El Problema Llega es a la Hora de Mostrar Los Productos En Una Tabla Lo Hago Con foreach El Los Muestra Pero Solo El Primer Resultado Es El Que Se Muestra dentro de la Tabla Los Demas Resultados se Muestran Fuera de la Tabla.


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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script type="text/javascript">
 
function ShowSelected()
{
 
var cod = document.getElementById("datos").value;
 
var i = cod.split(",");
var nom = cod.split(",");
var cant = cod.split(",");
var pre = cod.split(",");
 
var id = i[0];
var nombre = i[1];
var cantidad = i[2];
var precio = i[3];
 
document.getElementById("id").value=id;
document.getElementById("nombre").value=nombre;
document.getElementById("cantidad").value=cantidad;
document.getElementById("precio").value=precio;
}
</script>
</head>
<body>
 
 
<?php
 
include("conexion.php");
 
session_start();
 
if(isset($_POST['agregar']))
 
{
 
$id=$_POST['id'];
$producto=$_POST['nombre'];
$cantidad=$_POST['cant'];
$precio=$_POST['precio'];
 
$_SESSION['articulos'][$id]  =
  array('id_articulo'=>$id,
  		'producto'=>$producto,
		'cantidad'=>$cantidad,
		'precio'=>$precio
		);
echo "
<table border='1'>
<tr><td>Identificador</td><td>Nombre</td><td>Cantidad</td><td>Precio</td></tr>";
echo "<pre>"; print_r($_SESSION['articulos']); echo "</pre>";
foreach( $_SESSION['articulos'] as $fila){
 
 
echo "<tr> <td>".$fila['id_articulo']."</td>".
 
" <td>".$fila['producto']."</td>".
 
" <td>".$fila['cantidad']."</td>".
 
" <td>".$fila['precio']."</td> </tr> </table>";
 
 
}
 
}elseif (isset($_POST['borrar']))
{
	unset($_SESSION['articulos']);
}
 
 
 
?>
 
<br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="formulario" method="POST">
 
	<select name="datos" id="datos" onchange="ShowSelected();">
	<option></option>
		<option value="1,Hojas,21,50">1 | Hojas Blancas| 21 | 50</option>
		<option value="2,Borradores,100,300">2 | Borradores| 100 | 300</option>
	</select>
 
<br>
	<input type="text" name="id" id="id"><br>
	<input type="text" name="nombre" id="nombre"><br>
	<input type="text" name="cant" id="cantidad"><br>
	<input type="text" name="precio" id="precio"><br>
 
	<input type="submit" name="enviar">
	<button name="agregar">Agregar</button>
	<button name="borrar">Borrar</button>
</form>
 
 
</body>
</html>
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 Alejandro
Val: 575
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Mostrar Array en una Tabla HTML

Publicado por Alejandro (157 intervenciones) el 16/02/2017 07:38:10
Hola yonathan, prueba cerrar tu tabla después de la llave de cierre del foreach, despues que se agregan cada una de las filas cierras la tabla.

Cambia esto:
1
2
3
4
5
6
foreach( $_SESSION['articulos'] as $fila){
echo "<tr> <td>".$fila['id_articulo']."</td>".
" <td>".$fila['producto']."</td>".
" <td>".$fila['cantidad']."</td>".
" <td>".$fila['precio']."</td> </tr> </table>";
}

Por esto:

1
2
3
4
5
6
7
foreach( $_SESSION['articulos'] as $fila){
    echo "<tr> <td>".$fila['id_articulo']."</td>".
    " <td>".$fila['producto']."</td>".
    " <td>".$fila['cantidad']."</td>".
    " <td>".$fila['precio']."</td> </tr>";
    }
    echo "</table>";

Comentanos si te sirvio. Un Saludo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
3
Comentar
Imágen de perfil de yonathan

Mostrar Array en una Tabla HTML

Publicado por yonathan (2 intervenciones) el 16/02/2017 14:02:21
Muchas Gracias Alejandro Ese Era El Problema De Verdad Mil Gracias, Me Quitaste un Dolor de Cabeza..
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