【问题标题】:Jquery (Datatables) features not fully initializing? php/mysqlJquery(数据表)功能未完全初始化? php/mysql
【发布时间】:2015-03-27 19:37:46
【问题描述】:

我正在尝试应用 Jquery 的数据表来显示我的数据库。在此链接的示例中,表格是可搜索的、可排序的,并且具有很好的颜色分离。

http://www.datatables.net/examples/basic_init/zero_configuration.html

我当前的代码显示数据,但没有排序功能,没有搜索选项,只有原始数据、粗体标题和分隔行的条(没有像示例中那样交替颜色。)带有垃圾数据的图片:@987654322 @

似乎它正在应用一些 css 但不是全部?我不知道为什么缺少功能和剩余样式。我返回并从上一个代码中放入 html 结构,结果相同,没有抛出错误。

代码:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT Sid, Fname, Lname, Email, Dtype, Mac, Date FROM StudentDeviceReg";
$result = $conn->query($sql);
?>
<!Doctype html>
<html>
    <head>
        <title>DataTables</title>
        <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


        <script src="media/js/jquery-1.11.1.min.js" type="text/javascript"></script>
        <script src="media/js/jquery.dataTables.min.js" type="text/javascript"></script>

        <style type="text/css">
            @import "media/css/jquery.dataTables.css";
        </style>

        <script type="text/javascript" charset="utf-8">
            $(document).ready(function(){
            $('#datatables').dataTable();
            })
        </script>
    </head>
    <body>
        <div>
            <thead>

<?php

if ($result->num_rows > 0) {
    echo "<table id='datatables' class='display'>
    <tr>
        <th>ID</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>Device</th>
        <th>Mac Address</th>
        <th>Date</th>
    </tr>";
?>

            </thead>
            <tbody>

<?php


    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr>
        <td>".$row["Sid"]."</td>
        <td>".$row["Fname"]."</td>
        <td>".$row["Lname"]."</td>
        <td>".$row["Email"]."</td>
        <td>".$row["Dtype"]."</td>
        <td>".$row["Mac"]."</td>
        <td>".$row["Date"]."</td>
          </tr>";
    }
    echo "</table>";


} else {
    echo "0 results";
}
$conn->close();
?>
            </tbody>
        </div>
    </body>
</html>

【问题讨论】:

  • 控制台有什么错误吗?
  • 否定,未创建错误日志
  • 您确定您的表格结构良好吗?我建议你在浏览器中检查你的页面源代码:-) 你打印这个:&lt;thead&gt; &lt;table&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;/table&gt; &lt;/tbody&gt;(按照你的代码)
  • 我明白你在说什么,但考虑到表格是在 php 括号中开始并且必须以 echo 停止,而 jquery 需要 我想他们在哪里运作?我再次迷失了如何解决这个问题,我从未尝试过获取 jquery 和 php/mysql 列表。我不知道为什么它不会抛出任何错误并且只提供一些 CSS 就像它正在做的那样
  • @sylcat ,看看我的回答。

标签: php jquery html mysql css


【解决方案1】:

您的表树结构不正确。

<body>
<div>

<?php

 if ($result->num_rows > 0) {
   echo "
   <table id='datatables' class='display'>
    <thead>
      <tr>
        <th>ID</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>Device</th>
        <th>Mac Address</th>
        <th>Date</th>
      </tr>
    </thead>
    <tbody>";

    while($row = $result->fetch_assoc()) {
     echo "
      <tr>
        <td>".$row["Sid"]."</td>
        <td>".$row["Fname"]."</td>
        <td>".$row["Lname"]."</td>
        <td>".$row["Email"]."</td>
        <td>".$row["Dtype"]."</td>
        <td>".$row["Mac"]."</td>
        <td>".$row["Date"]."</td>
      </tr>";
    }
    echo "
    <tbody>
   </table>";
 }

?>

</div>
</body>

【讨论】:

  • 哦!我没有意识到你可以像这样在 php 中嵌套标签,仍然有点新。它现在正在工作,非常感谢! :)
猜你喜欢
  • 2021-09-07
  • 2016-07-16
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 2011-03-13
  • 1970-01-01
  • 2012-12-26
  • 2022-08-11
相关资源
最近更新 更多