PHP分页页码

时间:2013-11-21 21:31:29

标签: php pagination

我有几个网站用来处理分页的代码。我面临的问题是代码只提供“下一个”和“前一个”链接。对于我正在处理的网站,我也需要页码,不是全部,也许每次5个,这样的亲属 < 1 2 3 4 5> 然后当你到达第5页 < 6 7 8 9 10>

这是我的分页代码,直到

//paganation settings
$pages_query = mysqli_query($link, "SELECT COUNT(`id`) FROM `products` WHERE subcat = 1 AND 
                            status = 1") or die(mysqli_error($link));
$result = mysqli_fetch_array($pages_query, MYSQLI_NUM);
$pages = $result[0] / $per_page;
$page = (isset($_GET['page']) AND (int)$_GET['page'] > 0) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$last = ($pages - 1) / $per_page;
$prev = $page - 1;                                                      
$next = $page + 1; 
//query the db
$q =mysqli_query($link, "SELECT * FROM products WHERE subcat = 1 AND status = 1
          ORDER BY id DESC LIMIT    $start, $per_page");
if(mysqli_num_rows($q) > 0){
//find out the page we are on and display next and previous links accordingly
   if ($page >= 1){ 
      if ($page < $pages) {
         echo "<span class='next'>";
          echo "<a href=\"?page={$next}\">Next</a> ";
          echo "</span>";
      }
      if ($page >=2){
         echo "<span class='prev'>";
         echo "<a href=\"?page={$prev}\">Back</a> ";
         echo "</span>";
      }
}   
else if ($page > $pages + 1){
   echo 'No more images in the database';
}

任何人都可以帮我在这里添加页码

由于

1 个答案:

答案 0 :(得分:0)

for ($i=1; $i<=$total_pages; $i++) {  
         $pagLink .= "<a href='index.php?page=".$i."'>".$i."</a>";  
};

这可能对你有帮助 把它放在if下一个和上一页的循环之间