Home Backend Development PHP Tutorial PEAR探奇之PEAR::Pager_PHP

PEAR探奇之PEAR::Pager_PHP

Jun 01, 2016 pm 12:32 PM
br echo param var_dump return

  在页面上显示大量数据,是WEB项目中很常见的一个作法,但限于屏幕的大小,为了便于用户阅读,我们通常需要将数据分页显示,所以分页功能是大部份项目不可缺少的。PEAR::Pager是一个功能强大的分页类,使用非常方便。


  系统需求:PHP4.3.* /PHP5,PEAR::Pager


  PEAR::Pager有两种分页显示模式,一是Jumping,二是Sliding。两者有什么区别,我们来看两个例子:

Jumping.php


require_once 'Pager/Pager.php';

$params = array(
'mode' => 'Jumping',
'perPage' => 3,
'delta' => 5,
'itemData' => array('a','b','c','d','e','z','ty','xc','fg','fg','jk','hj','ty','xc','e','z','ty','xc','fg','fg','jk','hj','ty','xc')
);

echo "
当前分页模式:".$params['mode'];
echo "
每页显示数据条数:".$params['perPage'];
echo "
显示页数:".$params['delta'];
echo "
详细数据数组:";
print_r($params['itemData']);


$pager = & Pager::factory($params);
$data = $pager->getPageData();
$links = $pager->getLinks();


echo "

最后效果:";
echo $links['all'];
echo $pager->linkTags;

echo '

当前页的数据: ' ;
echo "

"; <br>print_r($data); <br>echo "
Copy after login
";

echo "其它类方法得到的数据:

";
echo 'getCurrentPageID()...: ';
var_dump($pager->getCurrentPageID());
echo "
";
echo 'getNextPageID()......: ';
var_dump($pager->getNextPageID());
echo "
";
echo 'getPreviousPageID()..: ';
var_dump($pager->getPreviousPageID());
echo "
";
echo 'numItems()...........: ';
var_dump($pager->numItems());
echo "
";
echo 'numPages()...........: ';
var_dump($pager->numPages());
echo "
";
echo 'isFirstPage()........: ';
var_dump($pager->isFirstPage());
echo "
";
echo 'isLastPage().........: ';
var_dump($pager->isLastPage());
echo "
";
echo 'isLastPageComplete().: ';
var_dump($pager->isLastPageComplete());
echo "
";
echo '$pager->range........: ';
var_dump($pager->range); echo "
";

?>


  运行结果如下图:

PEAR探奇之PEAR::Pager_PHP


Sliding.php

require_once 'Pager/Pager.php';

$month = 'september';

$params = array(
'mode' => 'Sliding',
'append' => false,
'urlVar' => 'num',
'path' => 'http://localhost/' . $month,
'fileName' => 'art%d.html', //%d将被替换成当前页的数字
'itemData' => array('a','b','c','d','e','z','ty','xc','fg','fg','jk','hj','ty','xc','e','z','ty','xc','fg','fg','jk','hj','ty','xc'),
'perPage' => 3
);

echo "
当前分页模式:".$params['mode'];
echo "
每页显示数据条数:".$params['perPage'];
echo "
链接指向路径:".$params['path'];
echo "
链接指向文件名:".$params['fileName'];
echo "
详细数据数组:";
print_r($params['itemData']);


$pager = & Pager::factory($params);

$data = $pager->getPageData();

echo "

最后效果:";
echo $pager->links;

echo '

当前页的数据: ' ;
echo "

"; <br>echo 'Data for current page: '; print_r($data); <br>echo "
Copy after login
";

echo "其它类方法得到的数据:

";
echo 'getCurrentPageID()...: ';
var_dump($pager->getCurrentPageID());
echo "
";
echo 'getNextPageID()......: ';
var_dump($pager->getNextPageID());
echo "
";
echo 'getPreviousPageID()..: ';
var_dump($pager->getPreviousPageID());
echo "
";
echo 'numItems()...........: ';
var_dump($pager->numItems());
echo "
";
echo 'numPages()...........: ';
var_dump($pager->numPages());
echo "
";
echo 'isFirstPage()........: ';
var_dump($pager->isFirstPage());
echo "
";
echo 'isLastPage().........: ';
var_dump($pager->isLastPage());
echo "
";
echo 'isLastPageComplete().: ';
var_dump($pager->isLastPageComplete());
echo "
";
echo '$pager->range........: ';
var_dump($pager->range); echo "
";
?>

运行结果如下图:

PEAR探奇之PEAR::Pager_PHP

  运行以上代码,可以发现,Jumping是跳跃式前进,一次翻几页,而Sliding是逐页地前进。

  在例子中可以看到PEAR::Pager提供了很多类方法,可以返回我们所需要的数据,如当前页ID,下一页ID,当前页上数据条数等等。

下面将所有类方法的作用列出,供参考:

Pager::Pager() – 构造函数 参数为二维数组 详见上面的示例

Pager::factory() -- 建立一个Pager对象

Pager::getCurrentPageID() 返回当前页的ID

Pager::getLinks() 返回某个页所对应的链接 参数为页面的ID 参数为空则返回当前页的链接

Pager::getNextPageID()  返回下一页的ID

Pager::getOffsetByPageId() 返回记录范围 

例如当前页为1,每页显示10条,则返回(1,10)

如当前页为2,每页显示8条,则返回(8,16)

Pager::getPageData() 以数组形式返回当前页数据 

Pager::getPageIdByOffset() 根据记录范围返回页面ID

本方法仅当分页模式为Jumping时有效

Pager::getPageRangeByPageId() 返回某个页面所在的记录范围

例如模式为Jumping时,如果pageId=3 , delta=10,则返回(1,10),如果pageId=3 , delta=10,则仍然返回(1,10),因为1,3都在10的范围之内。如果pageId =14,则返回(10,20),

因为14落于10,20之间。

Pager::getPreviousPageID() 得到前一页的ID

Pager::getperpageselectbox() 返回一个XHTML的Select标签字符串

可用于直接跳转到某个页面

Pager::isFirstPage() 判断当前页是否第一页

Pager::isLastPage() 判断当前页是否最后第一页

Pager::isLastPageComplete() 判断是否到达最后一页

Pager::numItems() 返回总记录数

Pager::numPages() 返回总页数


其它功能及用法请参看PEAR::Pager文档:

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP Tips: Quickly Implement Return to Previous Page Function PHP Tips: Quickly Implement Return to Previous Page Function Mar 09, 2024 am 08:21 AM

PHP Tips: Quickly implement the function of returning to the previous page. In web development, we often encounter the need to implement the function of returning to the previous page. Such operations can improve the user experience and make it easier for users to navigate between web pages. In PHP, we can achieve this function through some simple code. This article will introduce how to quickly implement the function of returning to the previous page and provide specific PHP code examples. In PHP, we can use $_SERVER['HTTP_REFERER'] to get the URL of the previous page

What results does MySQL return after inserting data? What results does MySQL return after inserting data? Mar 01, 2024 am 10:27 AM

MySQL is a widely used relational database management system for storing and managing data. When we want to insert new data into a database table, we usually use the INSERT statement. In MySQL, when the INSERT statement is executed to successfully insert data, a result will be returned, which is the result of the insertion operation. In this article, we will discuss in detail the results returned by MySQL after inserting data and provide some specific code examples. 1. The result returned after inserting data is in MySQL. When successfully executed

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

&quot;Go Language Development Essentials: 5 Popular Framework Recommendations&quot; As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

How to use Vue to implement the return to previous page effect How to use Vue to implement the return to previous page effect Sep 19, 2023 pm 01:07 PM

How to use Vue to implement the special effect of returning to the previous page. In front-end development, we often encounter situations where we need to return to the previous page. By adding a back button, you can provide a better user experience. This article will introduce how to use the Vue framework to achieve the special effect of returning to the previous page, and provide corresponding code examples. First, in the Vue project, you need to create a page as the previous page. We can set routing through VueRouter, and each route corresponds to a component. In the previous page, we can add a back button and pass the click event

Detailed explanation of the role and usage of the echo keyword in PHP Detailed explanation of the role and usage of the echo keyword in PHP Jun 28, 2023 pm 08:12 PM

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

Implementing distributed task scheduling using Golang's web framework Echo framework Implementing distributed task scheduling using Golang's web framework Echo framework Jun 24, 2023 am 11:49 AM

With the development of the Internet and the advancement of information technology, the era of big data has arrived, and fields such as data analysis and machine learning have also been widely used. In these fields, task scheduling is an inevitable problem. How to achieve efficient task scheduling is crucial to improving efficiency. In this article, we will introduce how to use Golang's web framework Echo framework to implement distributed task scheduling. 1. Introduction to the Echo framework Echo is a high-performance, scalable, lightweight GoWeb framework. It is based on HTTP

See all articles