在PHP中创建的表分页


Pagination in table that gets created in PHP

我有一个PHP文件,其中也有HTML/JS,在该文件内,我使用AJAX调用另一个PHP文件,该文件运行一些查询,然后将结果作为表返回到原始PHP文件。我想做的是对结果表进行分页。

搜索我发现这个https://stackoverflow.com/questions/19605078/how-to-use-pagination-on-html-tables#=旧的问题,所以我试图复制可接受的答案指向这个网站https://datatables.net/examples/basic_init/alt_pagination.html,但没有结果(甚至没有错误消息,这暗示我绝对缺乏对一些基本的理解)。

下面是我的代码在php文件中得到AJAX调用:

echo '<div class="table-responsive">';
echo '<table id="resultsTable" class="table table-hover table-condensed" align="center" style="width:80%">';
echo '<thead>';
echo '<tr>';
echo '<th>Πληροφορίες</th>';
echo '<th>Εξώφυλλο</th>';
echo "<td><a href='search.php?Order=1&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Τίτλος</button></a></td>";
echo "<td><a href='search.php?Order=2&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Συγγραφέας</button></a></td>";
echo "<td><a href='search.php?Order=3&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Εκδότης</button></a></td>";
echo "<td><a href='search.php?Order=4&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Είδος</button></a></td>";
echo "<td><a href='search.php?Order=5&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Θέμα</button></a></td>";
echo "<td><a href='search.php?Order=6&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>ISBN</button></a></td>";
echo '<th>Θέση</th>';
echo "<td><a href='search.php?Order=7&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Τωρινός Κάτοχος</button></a></td>";
echo "<td><a href='search.php?Order=8&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Προστέθηκε</button></a></td>";
if($LoggedIn == 1)
{
echo "<th>Αλλαγή Καταχώρησης</th>";
}
echo '</tr>';
echo '</thead>';
//body
echo '<tbody>';
for($i=0;$i<$counter;$i++)
{
echo ' <tr>';
    echo "<td href='#' onclick='"alert('".$ResultTable[$i][10]."');'" style='text-align: center;'><img src='IconImage.png' width='25' height='25' title='".$ResultTable[$i][10]."'/></td>";
    echo "<td><img src='BookCovers/". $ResultTable[$i][0]  ."' class='img-thubnail' alt='Δεν υπάρχει' width='50' height='50'/></td>";
    echo "<td>".$ResultTable[$i][1]."</td>";
    echo "<td>".$ResultTable[$i][2]."</td>";
    echo "<td>".$ResultTable[$i][3]."</td>";
    echo "<td>".$ResultTable[$i][4]."</td>";
    echo "<td>".$ResultTable[$i][5]."</td>";
    echo "<td>".$ResultTable[$i][6]."</td>";
    echo "<td>".$ResultTable[$i][7]."</td>";
    echo "<td>".$ResultTable[$i][8]."</td>";
    echo "<td>".$ResultTable[$i][9]."</td>";
    if($LoggedIn == 1)
    {
    echo "<td style='text-align: center;'><a href='UpdateEntry.php?BookID=".$ResultTable[$i][11]."' onclick='"window.open(this.href, 'mywin','left=20,top=20,width=500,height=500,toolbar=1,resizable=0'); return false;'"><button type='button' class='btn btn-info'><span class='glyphicon glyphicon-wrench'></span></button></a>
    <a href='DeleteBook.php?BookID=".$ResultTable[$i][11]."' onclick= '"return confirm('Διαγραφή Καταχώρησης; Αυτή η ενέργεια δεν μπορεί να αναιρεθεί!!')'"><button type='button' class='btn btn-danger'><span class='glyphicon glyphicon-remove'></span></button></a></td>";
    }
 echo '</tr>';
}echo '</tbody>';echo '</table>';echo "</div>";

在原来的php文件中,我在头块

中包含了以下内容
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">   
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src ="jquery-1.12.3.js"></script>
<script src ="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>

在正文结束前,我输入

  <script>$(document).ready(function() {
$('#resultsTable').DataTable( {
    "pagingType": "full_numbers"
} );} );</script>

当我打开页面时,结果显示正确,但表格根本没有格式化。

好了,我找到答案了。代码中的问题实际上是2.

    我已经包含了两个不同版本的jquery。创建表的AJAX调用是…异步的,所以执行流到达jquery部分时,表还没有构造。这就是为什么我从来没有在控制台中看到错误消息的原因。

我将jquery代码从head标签移动到AJAX调用的末尾,它正常工作。