如何创建分页器链接

发布于 2024-11-19 19:36:13 字数 260 浏览 2 评论 0原文

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

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

I am trying to create a dynamic page links created based on the number of rows in a mysql table. I would like to display 10 results per page and wish to have the php script create links to additional pages.

So I was thinking of using the num_rows and dividing it by 10 however if I have 53 rows the return would be 5.3 where as I would need 6 pages and not 5. I am thinking of using the round function and looping it through a for I statement until $pages > $rows_rounded. And every 10 rows add a link to pages($i) Is this the best method to acheive this or there an alternative simpler route to take?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归属感 2024-11-26 19:36:13

我制作的 pagenator 类。 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);
?>

pagenator class I made. getCurrentPages() returns all the pages you should be displaying in an array. so if you are on page one, and you want to display a total of 9 pages, you would get an array 1-9. if you were on page 10 however, your would get back an array 6-14. if there are 20 total pages and you are on page 20, you would get back an array 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 (USAGE):

<?php

   $pagenator = new Lev_Pagenator($current_page, $number_of_total_records, $records_per_page);
   $pages_array = $pagenator->getCurrentPages($number_of_pages_to_display);
?>
宫墨修音 2024-11-26 19:36:13

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 循环中,如果回显第三个链接,可能会添加一些额外的类以使您能够对其进行样式设置。

The idea of a for loop sounds like a good one, you would use something like:

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

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

But you need to consider detecting the current page, so if, for example, the current page was 3, it might be a good idea to test for that in your for loop and if echoing the 3rd link maybe add some extra class to enable you to style it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文