Right now I am working on a simple sql query through a search contained by a word stored in a variable, where I do a select and ask to show me the results in an html table, but the problem is that if that word has several records creates several tables for me and what I really want is that if the search returns several records, I place one record below the other without generating more html tables.
$query = "SELECT * FROM datos_usuarios WHERE LOWER(nombre_usuario) LIKE '%$palabra%' OR LOWER (apellido_usuario) LIKE '%$palabra%'";
$result = pg_query($query);
if ($palabra == null || $palabra =='')
echo "necesita escribir algo, para buscar";
else
{
$total = pg_num_rows($result);
if($total != 0)
{
while ($row = pg_fetch_array($result)) {
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Resultado de Busqueda</title>
<link rel="stylesheet" href="css/bootstra337.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<table class="table table-striped">
<thead>
<h3>Datos Usuario</h3>
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Apellido</th>
<th>Correo Electrónico</th>
</tr>
<tr>
</thead>
<tbody>
<td><?php echo "$row[0]"; ?></td>
<td><?php echo "$row[1]"; ?></td>
<td><?php echo "$row[2]"; ?></td>
<td><?php echo "$row[4]"; ?></td>
</tr>
</tbody>
</table>
<a href="admin.php" class="btn btn-primary" align="center">Atras</a>
</div>
</body>
</html>
<?php
}
}
else{
echo "No se encontraron resultados";
}
}
?>
I don't see it necessary, from my point of view, that you render the entire view for each record that the query returns (headers, css, tables, etc).
What you can do is run the
while
just below the tag<body>
and there you would have no problem displaying multiple records (or not displaying any at all).