Paginate a mysql table with DataTable

0

I have a table that brings mysql data in PHP, and I have the DataTable Scripts, but it does not page the table. What should I do to get the table, Thanks in advance.

<table>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <link href="../library/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
    <link href="../library/metisMenu/metisMenu.min.css" rel="stylesheet" type="text/css"/>
    <link href="../css/styles.css" rel="stylesheet" type="text/css"/>
    <link href="../library/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
    <link href="../library/datatables/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css"/>
    <link href="../library/datatables/css/dataTables.bootstrap4.min.css" rel="stylesheet" type="text/css"/>
    <link href="../library/datatables/css/dataTables.jqueryui.min.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="../library/fontawesome-free-5.0.4/fontawesome-free-5.0.4/svg-with-js/js/fontawesome-all.min.js"></script>
    <script type="text/javascript" src="../library/fontawesome-free-5.0.4/fontawesome-free-5.0.4/svg-with-js/js/fontawesome-all.js"></script>
    <script charset="utf8" type="text/javascript" src="../library/jquery/jquery-3.3.1.min.js"></script>
    <script src="../library/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="../library/metisMenu/metisMenu.min.js"></script>
    <script type="text/javascript" src="../library/datatables/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="../library/datatables/js/dataTables.bootstrap4.min.js"></script>
    <script type="text/javascript" src="../library/datatables/js/dataTables.jqueryui.min.js"></script>
    <script type="text/javascript" src="../js/js1.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#tablasExp').DataTable();
        });
    </script>
</head>

<body>
    <?php
        $consulta5 = "SELECT * FROM usuarios";
        echo "<div class='panel panel-default'>";
        echo "<div class='panel-heading'>";
        echo "Usuarios";
        echo "</div>";
        echo "<div class='panel-body'>";
        echo "<div class='table-responsive'>";

        echo "<table width='100%' class='table table-fixed table-bordered table-hover table-condensed table-hover' id='tablasExp' style='font-size: 11px;'>";
        echo "<thead style='background-color: #f8f8f8;'>";
        echo "<tr style='font-size: 13px; font-style: normal;'>";
        echo "<th style='text-align: center;'>Nombre</th>";
        echo "<th style='text-align: center;'>Identificacion</th>";
        echo "<th style='text-align: center;'>Nombre Usuario</th>";
        echo "</tr>";
        echo "</thead>";

        $matriz = mysqli_query($conexion, $consulta5);
        while ($fila = mysqli_fetch_row($matriz)) {
            echo "<tbody>";
            echo "<tr>";
            echo "<td>" . $fila[1] . "</td>";
            echo "<td>" . $fila[2] . "</td>";
            echo "<td>" . $fila[3] . "</td>";
            echo "</tr>";
            echo "</tbody>";
        }
    ?>
            </div>
        </div>
    </div>
</table>
</body>
    
asked by Kygo 26.01.2018 в 16:08
source

3 answers

0
  

The tbody tag has to open before the while and close after the close of the while.

By: FedericoG

    
answered by 26.01.2018 / 17:03
source
1

The <tbody></tbody> tags must be opened and closed before the while , otherwise you will create them many times and that is no longer recognized by the HTML , additional, you need to close the </table> and the close of the </div> are also in an erroneous place

Try this

<body>
    <?php
        $consulta5 = "SELECT * FROM usuarios";
        echo "<div class='panel panel-default'>";
        echo "<div class='panel-heading'>";
        echo "Usuarios";
        echo "</div>";
        echo "<div class='panel-body'>";
        echo "<div class='table-responsive'>";

        echo "<table width='100%' class='table table-fixed table-bordered table-hover table-condensed table-hover' id='tablasExp' style='font-size: 11px;'>";
        echo "<thead style='background-color: #f8f8f8;'>";
        echo "<tr style='font-size: 13px; font-style: normal;'>";
        echo "<th style='text-align: center;'>Nombre</th>";
        echo "<th style='text-align: center;'>Identificacion</th>";
        echo "<th style='text-align: center;'>Nombre Usuario</th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";
        $matriz = mysqli_query($conexion, $consulta5);
        while ($fila = mysqli_fetch_row($matriz)) {            
            echo "<tr>";
            echo "<td>" . $fila[1] . "</td>";
            echo "<td>" . $fila[2] . "</td>";
            echo "<td>" . $fila[3] . "</td>";
            echo "</tr>";            
        }
        echo "</tbody>";
        echo "</table>";
        echo "</div>";
        echo "</div>";
    ?>          
</body>

If it has been fixed, you can mark it as Answered : D

    
answered by 26.01.2018 в 17:19
0

You are missing the closing tag of the table, before the body tag.

    </table>
  </body>

Other divs that open before the table are also missing.

Edited:

The tbody tag has to open before the while and close after.

    
answered by 26.01.2018 в 16:14