【发布时间】:2016-01-08 21:47:05
【问题描述】:
我正在使用 PHP 填充我的表格,并使用 Datatable 进行分页和过滤器。我有一个向我的数据库发送值的 Android 应用程序。我想要的是对我的表进行实时更新。我尝试使用 AJAX 通过间隔调用 PHP 脚本使其成为实时。它工作正常,但分页和过滤器不起作用。
不要介意表格标题
HTML 代码
<table class="table table-bordered table-hover" id="myTable">
<thead>
<tr class="info">
<th>#</th>
<th>Resident</th>
<th>Complaints</th>
<th>Location</th>
<th>Date Issued</th>
</tr>
</thead>
<tbody id="comTbl">
</tbody>
</table>
<script>
JavaScript
$(document).ready(function() {
$("#comTbl").load("fetchPresident.php");
var refreshId = setInterval(function() {
$("#comTbl").load('fetchPresident.php?randval='+ Math.random());
}, 9000);
$.ajaxSetup({ cache: false });
});
</script>
<script>
$(document).ready(function() {
$('#myTable').DataTable( {
"lengthMenu": [[5, 10, 25, -1], [5, 10, 25, "All"]]
});
});
</script>
PHP
<?php
//Connect to database
$connection = mysqli_connect('localhost','root','','brm_dbs');
if (!$connection) {
die('Could not connect to database ' . mysqli_error($connection));
}
//Fetch the data from tables
$query="SELECT * FROM presidents";
$result = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row['f_name']."</td>
<td>" . $row['l_name'] . "</td>
<td>" . $row['party'] . "</td>
<td>" . $row['joined_office'] . "</td>
<td>" . $row['left_office'] . "</td>
</tr>";
}
// $output[] = array ("<tbody> <tr>","<td>",$row[0],"</td>","<td>",$row[1],"</td>","<td>",$row[2],"</td>","<td>",$row[3],"<td>","<td>",$row[4],"</td>","</tr> </tbdoy>");
//Close database connection
mysqli_close($connection);
?>
已尝试回显该表,但 数据表 无法读取 ID。 有人可以帮我解决这个问题吗?
【问题讨论】:
标签: php html mysql ajax datatables