【发布时间】:2016-03-10 22:18:12
【问题描述】:
如何使用 DataTables 显示 MYSQL 表中的数据 我试过了,但顶部显示给我
底部显示来自 MYSQL 表的记录。
我想让DataTables读取Mysql数据显示如下图
//bottstrap html代码
<div class="panel-body">
<div class="dataTable_wrapper">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Brand Name</th>
<th>Size</th>
</tr>
</thead>
// php
$record = mysqli_query($con,"SELECT * FROM ts127 ORDER BY Manufacturer");
while ($row = mysqli_fetch_array($record)) {
if()
{
}
elseif ($row['field1']==$XXX)
{
echo'<table style="table-layout:fixed;" class="table table-striped table-bordered table-hover" id="dataTables-example">';
// When i remove the above table code works fine for the first record or first row but other listed as a normal text
echo "<tr'>";
echo "<td style=' width:150px; text-align:left; padding: 10px;vertical-align: middle;'>";echo $row['Brand_Name'];echo"</td>";
echo "<td style='width:110px; text-align:left; vertical-align: middle;'>";echo $row['Size'];"</td>";
....
echo '<tr/></tbody></table>';
}
//JS代码
<script>
$(document).ready(function() {
$('#dataTables-example').DataTable({
responsive: true
});
});
</script>
【问题讨论】:
-
能否给我看看Js代码
-
@FahedAlkaabi 我把它添加到问题中或者你想看
jquery.dataTables.min.js的内容 -
如果我没记错的话,你做了很多PHP的事情并且代码没有完成。所以我不能判断你的PHP代码。 - Js 代码和 html 没有问题 - 在您的浏览器上检查开发工具(Google Chrome F12)上的控制台,看看是否有任何错误
-
你的 PHP 代码不清楚,这个 $XXX 或 $YYY 或 if 条件下的重置值是什么。同样在 while 循环中,您应该重复 tr 标签而不是 table 标签
-
@GoudaElalfy 是的,当我删除此行
echo'<table style="table-layout:fixed;" class="table table-striped table-bordered table-hover" id="dataTables-example">';时,它可以工作,但仅适用于第一行
标签: javascript php mysql twitter-bootstrap-3 datatable