2011-07-12 71 views
1

我想创建一个基于mysql表中的行数创建的动态页面链接。我想每页显示10个结果,并希望让php脚本创建指向其他页面的链接。如何创建分页器链接

所以我想使用num_rows并将其除以10然而,如果我有53行返回将是5.3,因为我需要6页而不是5我正在考虑使用循环函数并循环它通过一个for I语句直到$ pages> $ rows_round。并且每10行添加一个链接到页面($ i)这是实现这一目标的最佳方法,还是需要采用另一种更简单的路线?

+0

你想使用小区,不是圆的。 http://php.net/manual/en/function.ceil.php – dqhendricks

回答

0

的for循环的想法听起来不错的一个,你会使用类似:

$rows_rounded = ceil(mysql_num_rows($result)/10); 

for($x = 1; $x <= $rows_rounded; $x++){ 
    echo '<a href="/page-'.$x.'/'">Page '.$x.'</a>'; 
} 

但是你需要考虑检测当前页面,所以如果,例如,当前页面3 ,在for循环中测试它可能是一个好主意,如果回显第三个链接可能会添加一些额外的类来让您设置它的样式。

+0

我想我会指定$ x = $ current_page; $ x <= $ rows_round;然后从$ x = 1做另一个循环; $ x <= $ current_page ;.第二个循环会将页面#1写入当前页面,并首先写入从当前页面到最后页面的循环。我可以在循环中设置它的样式,并检查x = current_page,如果是,则设置它,否则打​​印一个正常的链接。我会_获取从gallery.php?page = 3等值...我认为这应该工作... – Drew

+0

其实上述是愚蠢的哈哈不想...是的,我可以检查循环中的当前页和它的风格... – Drew

1

pagenator class I made。 getCurrentPages()返回你应该在数组中显示的所有页面。所以如果你在第一页,并且你想要显示总共9页,你会得到一个数组1-9。如果你在第10页,但是你会得到一个数组6-14。如果总共有20个页面,并且你在第20页上,你会得到一个数组11-20。

<?php 

    class Lev_Pagenator { 

     private $recordsPerPage; 
     private $currentPage; 
     private $numberOfTotalRecords; 
     private $lastPage = null; 

     public function __construct($current_page, $number_of_total_records, $records_per_page = 25) { 
      $this->currentPage = $current_page; 
      $this->numberOfTotalRecords = $number_of_total_records; 
      $this->recordsPerPage = $records_per_page; 
     } 

     public function getCurrentStartIndex() { 
      return ($this->currentPage - 1) * $this->recordsPerPage; 
     } 

     public function getCurrentPages($number_of_pages_to_display = 9) { 
      $start_page = $this->currentPage - floor($number_of_pages_to_display/2); 
      if ($start_page < 1) $start_page = 1; 
      $last_page = $this->getLastPage(); 
      $pages = array($start_page); 
      for ($i = 1; $i < $number_of_pages_to_display; $i++) { 
       $temp_page = $start_page + $i; 
       if ($temp_page <= $last_page) { 
        $pages[] = $temp_page; 
       } else { 
        break; 
       } 
      } 
      return $pages; 
     } 

     public function getPreviousPage() { 
      if ($this->currentPage === 1) return false; 
      return $this->currentPage - 1; 
     } 

     public function getNextPage() { 
      if ($this->currentPage === $this->getLastPage) return false; 
      return $this->currentPage + 1; 
     } 

     public function getLastPage() { 
      if ($this->lastPage === null) $this->lastPage = ceil($this->numberOfTotalRecords/$this->recordsPerPage); 
      return $this->lastPage; 
     } 
    } 
?> 

EDIT(使用):

<?php 

    $pagenator = new Lev_Pagenator($current_page, $number_of_total_records, $records_per_page); 
    $pages_array = $pagenator->getCurrentPages($number_of_pages_to_display); 
?> 
+0

我将如何使用它?我需要指定任何值吗?或者干脆调用函数?这听起来正是我需要的,但我不知道如何实现它... – Drew

+0

@Drew以前从未使用过类?他们很好,你应该马上阅读!您甚至可以创建一个类自动加载器,只有在您尝试使用该类时才会加载包含您的类的脚本。你将不得不使用该类初始化一个对象,并给它一些输入(参数)。请参阅上面的编辑以获取快速使用提示还要注意,类中还有一些其他有用的方法,例如获取currentStartIndex的内容,您可以将它用作SQL语句中的开始记录。 – dqhendricks