Create HTML list with SQL query return

Asked

Viewed 844 times

0

I need to return the query SQL on a list HTML. In the precise case of line with "affiliate" and "name", the query below, returned correctly the requested data, but now I need to put this data one followed the other in a list HTML.

//iniciando a conexão com o banco de dados
$cx = mysqli_connect("localhost", "root", "");

//selecionando o banco de dados
$db = mysqli_select_db($cx, "dados");

//criando a query de consulta à tabela criada
$sql = mysqli_query($cx, "SELECT * FROM esocial") or die(
    mysqli_error($cx) //caso haja um erro na consulta
);

//pecorrendo os registros da consulta.
while($aux = mysqli_fetch_assoc($sql))
{
    echo "-----------------------------------------<br />";
    echo "Filial:".$aux["filial"]."<br />";
    echo "CPF:".$aux["cpfTrab"]."<br />";
    echo "PIS:".$aux["nisTrab"]."<br />";
    echo "Sexo:".$aux["sexo"]."<br />";
    echo "Raça/Cor:".$aux["racaCor"]."<br />";
    echo "Estado Civil:".$aux["estCiv"]."<br />";
    echo "Grau Instrução:".$aux["grauIntr"]."<br />";
    echo "Data Nascimento:".$aux["dtNascto1"]."<br />";
    echo "Cod. Município:".$aux["codMunic1"]."<br />";
    echo "UF:".$aux["uf1"]."<br />";
    echo "País Nascimento:".$aux["paisNascto"]."<br />";
    echo "País Nacionalidade:".$aux["paisNac"]."<br />";
    echo "Nome Mãe:".$aux["nmMae"]."<br />";
    echo "Nome Pai:".$aux["nmPai"]."<br />";
}

1 answer

1

Before the forech you open the list <ul> or <ol>, inside puts the data between <li> </li> and then closes </ul> or </ol>:

echo "<ul>";
while($aux = mysqli_fetch_assoc($sql)) {
    echo "<li> Filial:".$aux["filial"]." - Nome: ".$aux["nome"]."</li>";
}
echo "</ul>";

To show in a table just change the HTML tags:

<table>
    <colgroup>
        <col width="25%">
        <col width="50%">
        <col width="25%">
    </colgroup>
    <thead>
        <tr>
            <th>Nome</th>
            <th>CPF</th>
            <th>...</th>
        </tr>
    <thead>
    <tfoot>
        <tr>
            <td>Nome</td>
            <td>CPF</td>
            <td>...</td>
        </tr>
    <tfoot>
    <tbody>
        <?php
            while($aux = mysqli_fetch_assoc($sql)) {
                echo "
                    <tr>
                        <td>".$aux["nome"]."</td>
                        <td>".$aux["cpf"]."</td>
                        <td>".$aux["..."]."</td>
                    </tr>
                ";
            }
        ?>
    </tbody>
</table>

thead sets the table header, tbody the body of the table, tfoot the footer of the table and colgroup sets the style of the columns (you can change other HTML attributes, in case I just used to set the width of the column)

That?

  • Guilherme, the query is ok, what I need is to assemble a header with; BRANCH CPF PIS SEX RACA and so on, and each result is listed under the respective name. example; BRANCH CPF PIS SEXO 0000 XXX XXXX 1111 ZZZ ZZZ ZZZZ....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.