0

我有一个可以产生数千个结果的大型数据库,我需要限制分页链接的数量,比如 9 个链接,前 4 页在一侧,然后是当前页面,然后是 4 个下一页。

例如:14|15|16|17| 18 |19|20|21|22 有谁知道我如何做到这一点?谢谢

if (isset($_GET["page"])) 
  { 
    $page  = $_GET["page"];
  } 
    else 
  { 
    $page=1; 
  };

$start_from = ($page-1) * 10;
$message =  "SELECT * FROM document WHERE email='$_SESSION[email]' ORDER BY id DESC LIMIT $start_from , 10";

          // echo  results

         // make page links for results

  $sql = "SELECT id FROM document WHERE email = '$_SESSION[email]'";
  $rs_result = $db->query($sql);
  $total_records = $rs_result->num_rows;
  $total_pages = ceil($total_records / 10);

  if($rs_result->num_rows >10) {

$page = "<p class = 'page'>";
  for ($i=1; $i<=$total_pages; $i++) 
 {
   $page .="<a href='results.html?page=".$i."'>".$i."</a> ";
 }
   $page .="</p>";
   echo $page;
}
4

1 回答 1

2

只需使用基本数学来计算循环中使用的起始页和结束页:

$page_range_offset = 4;    
$page_start = $page - $page_range_offset;    
if ($page_start < 1) {
    $page_start = 1;
}    
$page_end = $page + $page_range_offset;
if ($page_end > $total_pages) {
    $page_end = $total_pages;
}

for ($i=$page_start; $i<=$page_end; $i++) {
   $page .="<a href='results.html?page=".$i."'>".$i."</a> ";
}
于 2013-02-12T00:40:57.090 回答