简体   繁体   English

使用MySQL填充jQuery DataTable

[英]Populate a jQuery DataTable with MySQL

I am attempting to populate a jQuery DataTable with information from a MySQL database without using AJAX. 我试图用MySQL数据库中的信息填充jQuery DataTable而不使用AJAX。 I would prefer to just use simpler code because this project is meant to be as minimal as possible. 我宁愿只使用更简单的代码,因为这个项目应尽可能少。 The following code is before the start of my HTML markup: 以下代码在我的HTML标记开始之前:

<?php
    //Connect to the MySQL database
    $usernameMain = "username"; 
    $passwordMain = "password"; 
    $hostMain = "host"; 
    $dbnameMain = "database";
    $optionsMain = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 
    $dbMain = new PDO("mysql:host={$hostMain};dbname={$dbnameMain};charset=utf8", $usernameMain, $passwordMain, $optionsMain);

    $sql = 'SELECT  name,date,location,entity,type FROM `events`';
    $result = $dbMain->query($sql);

?>

I have generated a connection to my database (I removed my details), and also ensured to import the script files for jQuery DataTables as well. 我已经生成了与我的数据库的连接(我删除了我的详细信息),并且还确保了导入jQuery DataTables的脚本文件。

Lower down in my HTML document, I created a table and attempted to echo out this database information with a while loop: 在我的HTML文档中,我创建了一个表,并尝试使用while循环回显这个数据库信息:

<table class="table table-hover table-nomargin table-bordered usertable"
    id="datatables">
        <thead>
            <tr class='thefilter'>
                <th class='with-checkbox'></th>
                <th>Name</th>
                <th>Location</th>
                <th>Type</th>
                <th>Date</th>
                <th>Entity</th>
                <th>Options</th>
            </tr>
            <tr>
                <th class='with-checkbox'><input id="check_all" name=
                "check_all" type="checkbox"></th>
                <th>Name</th>
                <th>Location</th>
                <th>Date</th>
                <th>Type</th>
                <th>Sponsoring Entity</th>
                <th>Options</th>
            </tr>
        </thead>

        <tbody>
            <!--Gather Rows from Database-->
            <?php
             $row = mysqli_fetch_array($result);
              while ($row)     {
             ?>

            <tr>
                <td class="with-checkbox"><input name="check" type="checkbox"
                value="1"></td>
                <td><?php echo $row['name']; ?></td>
                <td><?php echo $row['date']; ?></td>
                <td><?php echo $row['location']; ?></td>
                <td><?php echo $row['entity']; ?></td>
                <td><?php echo $row['type']; ?></td>
                <td>
                    <a class="btn icon-pencil" href="#" rel="tooltip" style=
                    "font-style: italic" title="Edit"></a> <a class=
                    "btn icon-plus-sign" href="#" rel="tooltip" style=
                    "font-style: italic" title="Enter Attendance"></a>
                </td>
            </tr>
            <?php
             }
            ?>
        </tbody>
    </table>

Then after this table was created, I inserted the following Javascript code to initialize the table as a DataTable with jQuery: 然后在创建此表之后,我插入了以下Javascript代码,以使用jQuery将表初始化为DataTable:

<script type="text/javascript">
(document).ready(function() {
    $('#datatables').dataTable( {
    } );
 );
</script>

I do not have appropriate reputation to upload images, so I have created an Imgur album with the database setup of my website and the error I get with the HTML page here: https://imgur.com/a/vacA4 我没有适当的声誉来上传图片,因此我创建了一张Imgur相册,其中包含我网站的数据库设置以及我在HTML页面中收到的错误: https//imgur.com/a/vacA4

I am not sure if the issue is database connection, DataTables, or something else. 我不确定问题是数据库连接,DataTables还是其他问题。 I have spent a lot of time on this issue and keep hitting roadblocks. 我花了很多时间在这个问题上并继续遇到障碍。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Thank you in advance for your assistance. 提前感谢您的协助。

You could just use the data option for dataTable, and feed the php data structure through the json_encode 您可以使用dataTable的data选项,并通过json_encode提供php数据结构

dataTable({
  ...
  data: <?phpecho json_encode($data_prepared_from_db); ?>
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM