I am programming in PHP
and MySQL
and I want to fill a simple table HTML
with a query. I have problems with foreach
, this is my code:
<table>
<tr>
<th>CODIGO</th>
<th>DESCRIPCION</th>
<th>GRUPO</th>
<th>SECCION</th>
<th>VIGENTE</th>
</tr>
<?php
if (isset($_POST["btnBuscar"])) {
$codigo = $_POST['txtCodigo'];
$ser->codigoservicio = $codigo;
$data = $sDriver->getByCodigo($codigo);
//print_r($data);
if(!empty($data)){
foreach ($data as $item) { ?>
<tr>
<td><?php echo $data->codigoservicio; ?></td>
<td><?php echo $data->descripcion; ?></td>
<td><?php echo $data->grupo; ?></td>
<td><?php echo $data->codigoseccion; ?></td>
<td><?php echo $data->vigente; ?></td>
</tr>
<?php
}
?>
</table>
<?php
}else{
echo 'No hay resultados.';
}
}
?>
This shows me the results but it repeats them 5 times each, and if I use:
<td><?php echo $item->codigoservicio; ?></td>
I get the following error:
Notice: Trying to get property of non-object in C: \ xampp \ htdocs ...
print_r($data);
Return me:
OaServicio Object (
[codigoservicio] => 12
[grupo] => 3
[descripcion] => prueba
[codigoseccion] => 1
[vigente] => 1
)
How can I do?