Recorrer arrays como objetos
Publicado por Lucas (46 intervenciones) el 08/05/2020 02:02:03
Hola, estoy teniendo unos problemas para recorrer unas arrays que indexé como objetos, alguien puede ver el fallo en el código? Use un código parecido para hacer un CRUD muy parecido y si funcionó.

Este es conexión.php:
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CRUD</title>
<?php
include ("conexion.php");
?>
</head>
<body>
<?php
$registros=$base->query("SELECT * FROM datos")->fetchAll(PDO::FETCH_OBJ);
?>
<form action="envio.php?planilla=Corte" method="post">
<table>
<tr>
<td>
<label for="fecha">Fecha:</label>
<input type="date" name="fecha">
</td>
<td>
<input type="text" name="lote">
</td>
</tr>
<tr>
<?php
foreach($registros as $fila):
?>
<tr>
<td><?php echo $fila->Nombre?></td>
<td><?php echo $fila->elemento1?></td>
<td><?php echo $fila->elemento2?></td>
<td><?php echo $fila->elemento3?></td>
<td><?php echo $fila->elemento4?></td>
<td><?php echo $fila->elemento5?></td>
<td class="bot"><a href="borrar.php?Id=<?php echo $fila->ID?>"><input type='button' name='borrar' value='Borrar'></a></td>
<td class='bot'><a href="editar.php?Id=<?php echo $fila->ID?> & nombre=<?php echo $fila->Nombre?>"><input type='button' name='actualizar' value='Actualizar'></a></td>
</tr>
<?php
endforeach;
?>
</tr>
<tr>
<td></td>
<td><input type='text' name='nombre'></td>
<td><input type='number' name='elemento1'></td>
<td><input type='number' name='elemento2'></td>
<td><input type='number' name='elemento3'></td>
<td><input type='number' name='elemento4'></td>
<td><input type='number' name='elemento5'></td>
<td class='bot'><input type='submit' name='insertar'value='Insertar'></td></tr>
</table>
</form>
</body>
</html>

Este es conexión.php:
1
2
3
4
5
6
7
8
9
10
<?php
try{
$base= new PDO ('mysql:host=localhost; dbname=formulario', 'root', '');
$base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$base->exec("SET CHARACTER SET UTF8");
}catch(Exception $e){
die('Error') . $e->getmessage();
echo "Linea del error" . $e->getLine();
}
?>
Valora esta pregunta


0