设置最大分页链接

时间:2013-02-12 00:31:23

标签: php html pagination

我有一个可以产生数千个结果的大型数据库,我需要限制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;
}

1 个答案:

答案 0 :(得分: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> ";
}