Paint a table with php

0

The result of this query I want to convert to a table, since the query is a value of the same field called name

  SELECT 
    id_departamento, departamento.nombre FROM 'departamento' 
  WHERE  
    departamento.id_raiz= '1'   

Throw each of the team names out there

id_Departamento     Nombre
             4      Domper/Desestibador
             5      Primera banda
             6      Primer mesa de seleccion
             7      Descarte de primer mesa

Etc .. are 42 names in total That information I want to show in a table

and I have this code

         <?php 
            include("dbconnect.php");
            $sql = $conn->prepare("SELECT id_departamento, 
            departamento.nombre FROM 'departamento' WHERE   
            departamento.id_raiz= '1'  ");
            $sql->execute();

        if ($sql->rowCount () > 0){

          echo "<table border = '1'> \n"; 
          while($rows=$sql->fetch(PDO::FETCH_ASSOC)){ 
              for ($i=0; $i < 6 ; $i++) {
                echo "<tr>";

              for ($j=0; $j < 7 ; $j++) {
                echo "<td>".$rows['nombre']."</td>";
                  }
                echo "</tr>";
              }
            }
            echo "</table>";
          }

         ?>

This code sends me this

I would like to know how I do it so that I do not repeat each row, but send me a box with all the information as it is but that is not repeated

    
asked by Eddy Olvera 06.02.2018 в 21:59
source

3 answers

0

You are using some loops that should not be there, that's why you are repeating not only the rows but also the fields, try this way:

echo "<table border = '1'> \n"; 
          while($rows=$sql->fetch(PDO::FETCH_ASSOC)){ 
                echo "<tr>";
                echo    "<td>".$rows['nombre']."</td>";
                echo    "<td>".$rows['apellido']."</td>";
                echo    "<td>".$rows['cedula']."</td>";
                echo    "<td>".$rows['etc']."</td>";
                echo    "<td>".$rows['etc1']."</td>";
                echo    "<td>".$rows['etc2']."</td>";
                echo "</tr>";
           }
echo "</table>";

that will print a row for each record of your table

EDITION:

I have added this because I think that what you want is for the table to be automatically formed with its rows and columns necessary according to the amount of elements extracted.

you indicate that they are 42, in which case it would be something like this:

echo "<table border = '1'>";
echo "<tr>";
          $con=1;
          while($rows=$sql->fetch(PDO::FETCH_ASSOC)){ 
                echo    "<td>".$rows['nombre']."</td>";
                if($con % 7==0){
                    echo "</tr><tr>";
                }
           $con++;
           }
echo "</tr>
</table>";

this would return a table more or less like this:

I hope it is what you are looking for. Greetings

    
answered by 06.02.2018 / 22:16
source
0

Yes, but I do not want you to send me the information in this way

if not that I form the table with this information, in case you send me the information in boxes not only in a row down

    
answered by 06.02.2018 в 22:37
0

At the end my code remained the following way

    
answered by 07.02.2018 в 23:21