Trying to implement data tables for first time.

Trying to implement data tables for first time.

dvitale71dvitale71 Posts: 1Questions: 1Answers: 0

Hi,
I'm new to data tables and am trying to implement for the first time. I trying to keep it simple for now using CDN but have no idea where to place my lines of code to execute data tables. I've pasted my code below for review. If someone could please take a peek and point me in the correct direction I would appreciate it. Thank you.

NOTE: I removed "<" from code so message would post.

DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
html>
link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-3.3.1.js"></script>
script
$(document).ready( function () {
$('#table_id').DataTable();
} );
</script>

tbody>

<?php php ?>

$hostname = "localhost";
$username = "root";
$password = "root";
$db = "domxadvg_channels";

$dbconnect=mysqli_connect($hostname,$username,$password,$db);

if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}

<?php > ?>

table id="table_id" class="display">
thead>

tr>
th>Category</th>
th>Channels</th>
/tr>

/thead>

<?php php ?>

$query = mysqli_query($dbconnect, "SELECT * FROM ChannelsList")
or die (mysqli_error($dbconnect));

while ($row = mysqli_fetch_array($query)) {
echo
"tr>
td>{$row['Category']}</td>
td>{$row['Channels']}</td>
/tr>\n";

}

<?php > /table> /tbody> /html> ?>

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    query.dataTables.js relies on jQuery so jquery-3.3.1.js needs to be loaded first. You can look at the examples and it will show the order of the include files:
    https://datatables.net/examples/index

    You can use the Download Builder to generate the CDN. The order listed is the order you should use.

    I'm not familiar with the PHP script but it looks like it might not be generating proper HTML. It has:

    thead>
    
    tr>
    th>Category</th>
    th>Channels</th>
    /tr>
    
    /thead>
    

    It looks like the leading < is missing from the HTML elements. I think it should look like this:

    <thead>
    
    <tr>
    <th>Category</th>
    <th>Channels</th>
    </tr>
    
    </thead>
    

    The same with the next section of code.

    Kevin

This discussion has been closed.