gpt4 book ai didi

javascript - 如何使用 While 循环使用 DataTables 显示 MYSQL 数据

转载 作者:行者123 更新时间:2023-11-28 23:42:36 24 4
gpt4 key购买 nike

如何使用 DataTables 显示 MYSQL 表中的数据 我试过了,但顶部显示给我 enter image description here

底部显示来自 MYSQL 表的记录。

我想让DataTables读取Mysql数据显示如下图

enter image description here

//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>

最佳答案

你的错误是循环表和 tbody,你应该更加小心打开和关闭 HTML 标签

<?php
$record = mysqli_query($con,"SELECT * FROM ts127 ORDER BY Manufacturer");
echo'<table style="table-layout:fixed;" class="table table-striped table-bordered table-hover" id="dataTables-example">';
echo '<thead>';
echo '<tr>';
echo '<th>Brand Name</th>';
echo '<th>Size/th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($record)) {
if (($row['field1']==$XXX || $row['field1']==$YYY ) && ($row['field2']>=$field1sw && $row['field1']<=$largesw) && ($txtbrand != "" && $size1=="" && $row['field2'] != "" && $row['Brand_Name']!="") )
{
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>';
}
}
echo '</tbody>';
echo '</table>';
?>

如果不是,我建议您使用 server-side DataTable

关于javascript - 如何使用 While 循环使用 DataTables 显示 MYSQL 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34114487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com