Sort PHP table

Asked

Viewed 919 times

-2

I am trying to sort a table that is in Php and that pulls the database data, tried with js called sorttable, but it did not work, the table is with this code:

            echo '<table class="sortable">'; 
            echo "<tr><th>Empresa</th><th>Atividade</th><th>Telefone</th><th>Licença</th><th>Contato</th><th>#</th></tr>";
            while ($row_empresas = mysqli_fetch_assoc($resultado_empresas)){
            echo '<tr>';
            echo '<td>' . $row_empresas['nomeCliente'] . '</td>';
            echo '<td>' . $row_empresas['documento'] . '</td>';
            echo '<td>' . $row_empresas['telefone'] . '</td>';
            echo '<td>' . $row_empresas['cidade'] . '</td>';
            echo '<td>' . $row_empresas['email'] . '</td>';
            echo '<td>';
                echo '<a href="'.base_url().'index.php/base2/visualizar/'.$row_empresas['idClientes'].'" style="margin-right: 1%" class="btn tip-top" title="Ver mais detalhes"><i class="icon-eye-open"></i></a>';
                echo '<a href="'.base_url().'index.php/base2/editar/'.$row_empresas['idClientes'].'" style="margin-right: 1%" class="btn btn-info tip-top" title="Editar Cliente"><i class="icon-pencil icon-white"></i></a>';
            echo '</td>';
            echo '</tr>';

            }
            echo '</table>';    

How could I arrange the same so that I could be ordained?

  • 1

    You can’t order the table with ORDER BY?

  • So, but after she’s got the data inside the table, I wish I could sort through it, not by order by, I don’t know if I was too clear

  • It may not be a solution but this can help you. It may not be what you asked for but Datatables can offer you many options.

1 answer

2

I use this Sorttable, example:

<html>
    <head>
        <title>Sorttable</title>
        <!-- Importa o JS -->
        <script type="text/javascript" src="https://kryogenix.org/code/browser/sorttable/sorttable.js"></script>
    </head>
    <body>
        <!-- Ativa o sorttable na tabela -->
        <table class="sortable">
            <thead>
                <tr><th>Person</th><th>Monthly pay</th></tr>
            </thead>
            <tbody>
                <tr><td>Jan Molby</td><td>£12,000</td></tr>
                <tr><td>Steve Nicol</td><td>£8,500</td></tr>
                <tr><td>Steve McMahon</td><td>£9,200</td></tr>
                <tr><td>John Barnes</td><td>£15,300</td></tr>
            </tbody>
            <tfoot>
                <tr><td>TOTAL</td><td>£45,000</td></tr>
            </tfoot>
          </table>
    </body>

It is very simple to use, and in the documentation it has more advanced uses (make it not sort by a certain column, keep icons visible, change icons, ...) and explanations

Browser other questions tagged

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