【问题标题】:How can i display MYSQL Data using DataTables using While Loop如何使用 While 循环使用 DataTables 显示 MYSQL 数据
【发布时间】: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'&lt;table style="table-layout:fixed;" class="table table-striped table-bordered table-hover" id="dataTables-example"&gt;'; 时,它可以工作,但仅适用于第一行

标签: javascript php mysql twitter-bootstrap-3 datatable


【解决方案1】:

你的错误是循环 table 和 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

【讨论】:

  • 仅对第一条记录有效。
  • 所以你需要仔细检查你的 if 语句
  • 我忘记添加Thead了,请看一下
  • 但是标题和分页上的排序图标不见了
  • stackoverflow.com/a/26564457/5154084这里是答案,如果请求良好,请尝试检查排序图标是否存在
【解决方案2】:

更新这个

echo '<tr/></tbody></table>';

成为

echo '</tr>';

然后在while循环外面关闭tbody和table标签

【讨论】:

  • 你的意思是 echo '';
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 2015-11-17
  • 2012-04-23
  • 2019-01-21
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
相关资源
最近更新 更多