首页 > 解决方案 > 分页链接不显示所有页面

问题描述

我正在尝试从 WordPress 数据库表中读取数据并将其显示在我的网站上。这是我的分页脚本,我能够获取所有数据。每页的限制和行数都很好。当我更改页面时,我仍然看到第一页,但 url 发生了变化。请问我该如何处理?谢谢

<?php
global $wpdb;
$er = $wpdb->show_errors();
$result = $wpdb->get_results("SELECT * FROM koh_user");

// number of rows to show per page
$rowsperpage = 10;

// find out how many rows are in the table 
$numrows = $wpdb->num_rows;

// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
   // cast var as int
   $page = (int) $_GET['page'];
} else {
   // default page num
   $page = 1;
} 

// the offset of the list, based on current page 
$offset = ($page - 1) * $rowsperpage;

// get the info from the db 
$result = $wpdb->get_results("SELECT CONCAT( koh_user.fname, '  ', koh_user.lname) as name, koh_user.id as id, koh_user.username as username from koh_user LIMIT $rowsperpage OFFSET $offset
");

// Display the rows 
echo "<style>";
echo "body {font-family: Arial;}";
echo ".table_container { padding: 10px 12px 0px 12px;  border: 1px solid #ccc;  }";
echo ".table_container th { background-color:lightblue; color:black; font-weight:bold; border-left: 1px solid white;}";
echo "</style></head>";
echo "<body>";
echo "<div class=\"table_container\"><table>";
echo "<tr><th style=\"padding-left:10px;\">Nom</th><th>Nom d'utilisateur</th><th>Role</th><th></th></tr>";

foreach ($result as $row) {
echo "<tr class ='info'>
<td>" . $row->name . "</td>
<td>" . $row->username . "</td>
<td>" . $row->role . "</td>
<td>
</td>
</tr>"
;}
echo "</table></div>";

// loop to show links to range of pages around current page
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($page > 1) {
   // show << link to go back to page 1
   echo " <a href='/?page_id=347?page=1'><<</a>  ";
   // get previous page num
   $prevpage = $page - 1;
   // show < link to go back to 1 page
   echo " <a href='/?page_id=347?page=$prevpage'><</a>  ";
} // end if 

// loop to show links to range of pages around  page
for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on  page...
      if ($x == $page) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='/?page_id=347?page=$x'>$x</a>  ";
      } // end else
   } // end if 
} // end for

// if not on last page, show forward and last page links        
if ($page != $totalpages) {
   // get next page
   $nextpage = $page + 1;
    // echo forward link for next page 
   echo " <a href='/?page_id=347?page=$nextpage'>></a>  ";
   // echo forward link for lastpage
   echo " <a href='/?page_id=347?page=$totalpages'>>></a>  ";
} // end if

?>

标签: phpsqldatabasewordpresspagination

解决方案


推荐阅读