如何paginator链接创建


how to pageinator link creation

我正在尝试创建一个基于mysql表中行数创建的动态页面链接。我想显示每页10个结果,并希望有php脚本创建链接到其他页面。

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

我制作的分页器类。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;
        }
    }
?>

编辑(使用):

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

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循环中测试,如果呼应第三个链接可能会添加一些额外的类,使你能够样式