PHP分页类

发布于 2024-08-25 08:36:36 字数 105 浏览 5 评论 0原文

我正在寻找一个 php 分页类,我过去使用过一个相当简单的类,但它不再受支持。

我想知道是否有人有任何建议?

当可能有这么多好的产品时,建立自己的产品似乎毫无意义。

I am looking for a php pagination class, I have used a rather simple one in the past and it is no longer supported.

I was wondering if anyone had any recommendations ?

It seems pointless to build my own when there are probably so many good ones out there.

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

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

发布评论

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

评论(7

哽咽笑 2024-09-01 08:38:03

您的 SQL SELECT 语句查询很可能将 1000 个结果转化为数千条记录。但将所有结果显示在一页上并不是一个好主意。所以我们可以根据需要将这个结果分成很多页,作为 pagination Class 。

使用 PAGINATION 类对数据进行分页 非常简单

pagination 类有助于生成分页

如何使用 Pagination 类
访问此链接了解更多信息
http://utlearn.com/2017/02/15 /分页类-使用分页类/

<?php

/**
 * @package pagination class
 * @version 1.0
 */
/*
@class Name: pagination 
@Author: Ahmed Mohamed
@Version: 1.0
@Author URI: https://www.fb.com/100002349977660
@Website URI: http://www.utlearn.com
@class page URI:  http://utlearn.com/2017/02/15/pagination-class-use-pagination-class
*/


include_once 'libs/config.php';
include_once 'libs/Database.php';
include_once 'libs/Model.php';
include_once 'libs/pagination.php';
if(!empty($_GET["page"]) and is_numeric($_GET["page"])){
    $page = htmlspecialchars(strip_tags($_GET["page"]));
}  else {
    $page = 1;
}
// news = table name / you page URL / current page / true or false for full query
// its false i just use table name  
$pag = new pagination("news", URL."?page=", 3, $page, false);
$pagination = $pag->pagination();
$data = $pag->data();

?>
<news>
    <?php foreach($data as $news){ ?>
    <header><h1><?=$news->title ?></h1> | <span><?=$news->date ?></span></header>
    <div>
        <?=$news->content ?>
    </div>
    <?php } ?>
</news>
<?=$pagination ?>

Its Very possible that your SQL SELECT statement query may 1000 result into thousand of records. But its is not good idea to display all the results on one page. So we can divide this result into many pages as per requirement as pagination Class .

PAGINATE DATA WITH PAGINATION CLASS VERY EASY

pagination Class helps to generate paging

How To Use Pagination Class
visit this link for more info
http://utlearn.com/2017/02/15/pagination-class-use-pagination-class/

<?php

/**
 * @package pagination class
 * @version 1.0
 */
/*
@class Name: pagination 
@Author: Ahmed Mohamed
@Version: 1.0
@Author URI: https://www.fb.com/100002349977660
@Website URI: http://www.utlearn.com
@class page URI:  http://utlearn.com/2017/02/15/pagination-class-use-pagination-class
*/


include_once 'libs/config.php';
include_once 'libs/Database.php';
include_once 'libs/Model.php';
include_once 'libs/pagination.php';
if(!empty($_GET["page"]) and is_numeric($_GET["page"])){
    $page = htmlspecialchars(strip_tags($_GET["page"]));
}  else {
    $page = 1;
}
// news = table name / you page URL / current page / true or false for full query
// its false i just use table name  
$pag = new pagination("news", URL."?page=", 3, $page, false);
$pagination = $pag->pagination();
$data = $pag->data();

?>
<news>
    <?php foreach($data as $news){ ?>
    <header><h1><?=$news->title ?></h1> | <span><?=$news->date ?></span></header>
    <div>
        <?=$news->content ?>
    </div>
    <?php } ?>
</news>
<?=$pagination ?>
葬﹪忆之殇 2024-09-01 08:38:02
public function make_pagination()
    {

    $total = 0;
    $query = "SELECT COUNT(downloads.dn_id) FROM downloads WHERE downloads.dn_type = 'audios'";
    $stmt = $this->conn->prepare($query);
    $stmt->execute();
    $total = $stmt->fetchColumn();
    //echo 'row_count = ' . $total;

    // How many items to list per page
    $limit = 11;

    // How many pages will there be
    $pages = ceil($total / $limit);

    // What page are we currently on?
    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
        'options' => array(
            'default'   => 1,
            'min_range' => 1,
        ),
    )));

    // Calculate the offset for the query
    $offset = ($page - 1)  * $limit;

    // Some information to display to the user
    $start = $offset + 1;
    $end = min(($offset + $limit), $total);

    // The "back" link
    $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">«</a> <a href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
    // The "forward" link
    $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>';
    // Display the paging information
    echo '<div id="paging"><p>'.$prevlink.' Page '.$page.' of '.$pages. ' pages'. $nextlink.' </p></div>';


    //prepare the page query
    $query2 = "  
                SELECT * FROM downloads, map_artists, song_artists
                WHERE map_artists.dn_id = downloads.dn_id 
                AND song_artists.artist_id = map_artists.artist_id 
                AND downloads.dn_type = 'audios' GROUP BY downloads.dn_id 
                ORDER BY downloads.dn_time DESC LIMIT :limit OFFSET  :offset ";


    $stmt2 = $this->conn->prepare($query2);
    $stmt2->bindParam(':limit', $limit, PDO::PARAM_INT);
    $stmt2->bindParam(':offset', $offset, PDO::PARAM_INT);
    $stmt2->execute();

    // Do we have any results?
    if ($stmt2->rowCount() > 0) {
        // Define how we want to fetch the results
        $stmt2->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($stmt2);

        // Display the results
        foreach ($iterator as $row) {
            echo '<p>'. $row['dn_title'].' - '. $row['artist_name'].'</p>';
        }

    } else {
        echo '<p>No results could be displayed.</p>';
    }



}
public function make_pagination()
    {

    $total = 0;
    $query = "SELECT COUNT(downloads.dn_id) FROM downloads WHERE downloads.dn_type = 'audios'";
    $stmt = $this->conn->prepare($query);
    $stmt->execute();
    $total = $stmt->fetchColumn();
    //echo 'row_count = ' . $total;

    // How many items to list per page
    $limit = 11;

    // How many pages will there be
    $pages = ceil($total / $limit);

    // What page are we currently on?
    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
        'options' => array(
            'default'   => 1,
            'min_range' => 1,
        ),
    )));

    // Calculate the offset for the query
    $offset = ($page - 1)  * $limit;

    // Some information to display to the user
    $start = $offset + 1;
    $end = min(($offset + $limit), $total);

    // The "back" link
    $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">«</a> <a href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
    // The "forward" link
    $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>';
    // Display the paging information
    echo '<div id="paging"><p>'.$prevlink.' Page '.$page.' of '.$pages. ' pages'. $nextlink.' </p></div>';


    //prepare the page query
    $query2 = "  
                SELECT * FROM downloads, map_artists, song_artists
                WHERE map_artists.dn_id = downloads.dn_id 
                AND song_artists.artist_id = map_artists.artist_id 
                AND downloads.dn_type = 'audios' GROUP BY downloads.dn_id 
                ORDER BY downloads.dn_time DESC LIMIT :limit OFFSET  :offset ";


    $stmt2 = $this->conn->prepare($query2);
    $stmt2->bindParam(':limit', $limit, PDO::PARAM_INT);
    $stmt2->bindParam(':offset', $offset, PDO::PARAM_INT);
    $stmt2->execute();

    // Do we have any results?
    if ($stmt2->rowCount() > 0) {
        // Define how we want to fetch the results
        $stmt2->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($stmt2);

        // Display the results
        foreach ($iterator as $row) {
            echo '<p>'. $row['dn_title'].' - '. $row['artist_name'].'</p>';
        }

    } else {
        echo '<p>No results could be displayed.</p>';
    }



}
鸵鸟症 2024-09-01 08:38:01
// pagination class
class Pagination
{
    // database handle
    private $dbh;

    // total records in table
    private $total_records;

    // limit of items per page
    private $limit;

    // total number of pages needed
    private $total_pages;

    // first and back links
    private $firstBack;

    // next and last links
    private $nextLast;

    // where are we among all pages?
    private $where;

    public function __construct($dbh) {
        $this->dbh = $dbh;
    }

    // determines the total number of records in table
    public function totalRecords($query, array $params)
    {
        $stmt = $this->dbh->prepare($query);
        $stmt->execute($params);
        $this->total_records = $stmt->fetchAll(PDO::FETCH_COLUMN)[0];

        if (!$this->total_records) {
            echo 'No records found!';
            return;
        }
    }

    // sets limit and number of pages
    public function setLimit($limit)
    {
        $this->limit = $limit;

        // determines how many pages there will be
        if (!empty($this->total_records)) {
            $this->total_pages = ceil($this->total_records / $this->limit);
        }
    }

    // determine what the current page is also, it returns the current page
    public function page()
    {
        $pageno =  (int)(isset($_GET['pageno'])) ? $_GET['pageno'] : $pageno = 1;

        // out of range check
        if ($pageno > $this->total_pages) {
            $pageno = $this->total_pages;
        } elseif ($pageno < 1) {
            $pageno = 1;
        }

        // links
        if ($pageno > 1) {
            // backtrack
            $prevpage = $pageno -1;

            // 'first' and 'back' links
            $this->firstBack = "<div class='first-back'><a href='$_SERVER[PHP_SELF]?pageno=1'>First</a> <a href='$_SERVER[PHP_SELF]?pageno=$prevpage'>Back</a></div>";
        }

        $this->where =  "<div class='page-count'>(Page $pageno of $this->total_pages)</div>";

        if ($pageno < $this->total_pages) {
            // forward
            $nextpage = $pageno + 1;

            // 'next' and 'last' links
            $this->nextLast =  "<div class='next-last'><a href='$_SERVER[PHP_SELF]?pageno=$nextpage'>Next</a> <a href='$_SERVER[PHP_SELF]?pageno=$this->total_pages'>Last</a></div>";
        }

        return $pageno;
    }

    // get first and back links
    public function firstBack()
    {
        return $this->firstBack;
    }

    // get next and last links
    public function nextLast()
    {
        return $this->nextLast;
    }

    // get where we are among pages
    public function where()
    {
        return $this->where;
    }
}

使用:

$pagination = new Pagination($dbh);
$pagination->totalRecords('SELECT COUNT(*) FROM `photos` WHERE `user` = :user', array(':user' => $_SESSION['id']));
$pagination->setLimit(12);
$pagination->page();
echo $pagination->firstBack();
echo $pagination->where();
echo $pagination->nextLast();

结果:

<div class='first-back'><a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=1'>First</a> <a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=3'>Back</a></div>
<div class='page-count'>(Page 4 of 6)</div>
<div class='next-last'><a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=5'>Next</a> <a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=6'>Last</a></div>
// pagination class
class Pagination
{
    // database handle
    private $dbh;

    // total records in table
    private $total_records;

    // limit of items per page
    private $limit;

    // total number of pages needed
    private $total_pages;

    // first and back links
    private $firstBack;

    // next and last links
    private $nextLast;

    // where are we among all pages?
    private $where;

    public function __construct($dbh) {
        $this->dbh = $dbh;
    }

    // determines the total number of records in table
    public function totalRecords($query, array $params)
    {
        $stmt = $this->dbh->prepare($query);
        $stmt->execute($params);
        $this->total_records = $stmt->fetchAll(PDO::FETCH_COLUMN)[0];

        if (!$this->total_records) {
            echo 'No records found!';
            return;
        }
    }

    // sets limit and number of pages
    public function setLimit($limit)
    {
        $this->limit = $limit;

        // determines how many pages there will be
        if (!empty($this->total_records)) {
            $this->total_pages = ceil($this->total_records / $this->limit);
        }
    }

    // determine what the current page is also, it returns the current page
    public function page()
    {
        $pageno =  (int)(isset($_GET['pageno'])) ? $_GET['pageno'] : $pageno = 1;

        // out of range check
        if ($pageno > $this->total_pages) {
            $pageno = $this->total_pages;
        } elseif ($pageno < 1) {
            $pageno = 1;
        }

        // links
        if ($pageno > 1) {
            // backtrack
            $prevpage = $pageno -1;

            // 'first' and 'back' links
            $this->firstBack = "<div class='first-back'><a href='$_SERVER[PHP_SELF]?pageno=1'>First</a> <a href='$_SERVER[PHP_SELF]?pageno=$prevpage'>Back</a></div>";
        }

        $this->where =  "<div class='page-count'>(Page $pageno of $this->total_pages)</div>";

        if ($pageno < $this->total_pages) {
            // forward
            $nextpage = $pageno + 1;

            // 'next' and 'last' links
            $this->nextLast =  "<div class='next-last'><a href='$_SERVER[PHP_SELF]?pageno=$nextpage'>Next</a> <a href='$_SERVER[PHP_SELF]?pageno=$this->total_pages'>Last</a></div>";
        }

        return $pageno;
    }

    // get first and back links
    public function firstBack()
    {
        return $this->firstBack;
    }

    // get next and last links
    public function nextLast()
    {
        return $this->nextLast;
    }

    // get where we are among pages
    public function where()
    {
        return $this->where;
    }
}

Use:

$pagination = new Pagination($dbh);
$pagination->totalRecords('SELECT COUNT(*) FROM `photos` WHERE `user` = :user', array(':user' => $_SESSION['id']));
$pagination->setLimit(12);
$pagination->page();
echo $pagination->firstBack();
echo $pagination->where();
echo $pagination->nextLast();

Result:

<div class='first-back'><a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=1'>First</a> <a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=3'>Back</a></div>
<div class='page-count'>(Page 4 of 6)</div>
<div class='next-last'><a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=5'>Next</a> <a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=6'>Last</a></div>
月下客 2024-09-01 08:38:00

你可以试试这个:
Zebra_Pagination,一个用 PHP 编写的通用、Twitter Bootstrap 兼容的分页类
检查下面的链接:
http://stefangabos.ro/php-libraries/zebra-pagination

you can try this:
Zebra_Pagination, a generic, Twitter Bootstrap compatible, pagination class written in PHP
check the link below:
http://stefangabos.ro/php-libraries/zebra-pagination

翻了热茶 2024-09-01 08:37:58

您尝试过 PEAR::Pager 吗?用法示例此处

Have you tried PEAR::Pager? Usage examples here.

清风挽心 2024-09-01 08:37:57

我建议 Zend_Paginator ,原因如下:

  • 它是松散耦合的,并且不不需要整个库。
  • ZF 社区比 PEAR 社区规模更大,并且正在积极对代码进行安全审计,并发布维护版本。
  • 它使用适配器模式来分离数据源,文档中有大量前端 UI 模式实现的示例。

I would suggest Zend_Paginator for the following reasons

  • It's loosely coupled and doesn't require the entire library.
  • The ZF community is larger than the PEAR community and is actively running security audits on code, and releasing maintenance versions.
  • It separates data sources by using the Adapter Pattern, and there are numerous examples of front end UI pattern implementations in the documentation.
不再让梦枯萎 2024-09-01 08:37:53

经过更多搜索后,我决定在使用框架版本之前,我应该充分了解分页器所涉及的内容。所以我自己建了一个。不过还是谢谢你的建议!

After more searching I decided that before I use a frameworked version I should fully understand what is involved in a paginator. So I built one myself. Thanks for the suggestions though!

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