SQL queries shown in a html + php table

2

At the moment I am working on a simple sql query through a search contained by a word stored in a variable, where I make a select and ask that I show the results in a table in html, but the problem is that if that word has several records creates several tables and what I really want is that if the search throws several records I put 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";
    }
}
?>
    
asked by Luis Alfredo Serrano Díaz 09.04.2018 в 00:56
source

1 answer

1

I do not see it necessary from my point of view, that you render the whole view for each record that returns the query (headers, css, tables, etc.).

What you can do is to run the while just below the <body> tag and there would be no problem showing several records (or not showing any).

<!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">
      <?php
      $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) {
          ?>
          <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>
              <?php
              while ($row = pg_fetch_array($result)) {
              ?>
                <tr>
                  <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>
              <?php
              }
              ?>
              </tbody>
          </table>
          <a href="admin.php" class="btn btn-primary" align="center">Atras</a>
          <?php
        } else {
          echo "No se encontraron resultados";
        }
      }
      ?>
      </div>
  </body>
</html>
    
answered by 09.04.2018 / 05:40
source