Very useful ZendFramework paging class _ PHP Tutorial

Source: Internet
Author: User
Very useful ZendFramework paging class. Here we will share with you a very useful page effect of ZendFramework. the page effect of this site can be seen, and the CSS style can be changed according to your personal design. Here I will give an example to share with you a very useful Zend Framework paging class.

The specific effect can be seen in the page effect of this site. the CSS style can be changed according to the personal design sense.

Here I will give an example to demonstrate how to use this class, as shown below:

IndexController. php:

The code is as follows:


Protected $ _ curPage = 1; // The first page by default
Const PERPAGENUM = 4; // number of entries per page

Public function indexAction ()
{
// $ This-> _ blogModel the blog Model has been instantiated.
// $ Rows-> obtain the total number of entries of the displayed data
$ Rows = $ this-> _ blogModel-> getTotalRows ();

If ($ pageNum = $ this-> getRequest ()-> getParam ('page ')){
// If a value is input, overwrite the first page
$ This-> _ curPage = $ pageNum;
}

// Upload data from the data table to the front end
$ This-> view-> blogInfo = $ this-> _ blogModel-> getBlogInfo (
Self: PERPAGENUM, ($ this-> _ curPage-1) * self: PERPAGENUM
);
// Instantiate the paging class and upload it to the front end
$ This-> view-> pagebar = $ this-> displayPageBar ($ rows );
}

Private function displayPageBar ($ totalRows)
{
$ Pager = new Zend_Pagination ($ totalRows, self: PERPAGENUM );
Return $ Pager-> getNavigation ();
}

Models/Blog. php:

The code is as follows:


Public function getBlogInfo ($ perPageNum = NULL, $ limit = NULL)
{
Return $ this-> fetchAll ('1 = 1', 'blog _ id desc ', $ perPageNum, $ limit)
-> ToArray ();
}

Public function getTotalRows ($ where = '1 = 1 ')
{
Return $ this-> fetchAll ($ where)-> count ();
}

Index. phtml, write the following code:

The code is as follows:



Pagebar;?>


Now, you can see the effect. to achieve better Page effects, modify the paging class based on your preferences.

The code is as follows:


Class Zend_Pagination
{
Private $ _ navigationItemCount = 6; // The total number of pages in the navigation bar is displayed.
Private $ _ pageSize = null; // number of projects per page
Private $ _ align = "right"; // location displayed on the navigation bar
Private $ _ itemCount = null; // total number of items
Private $ _ pageCount = null; // total number of pages
Private $ _ currentPage = null; // current page
Private $ _ front = null; // front-end controller
Private $ _ PageParaName = "page"; // page parameter name

Private $ _ firstPageString = "| <"; // the character displayed on the first page of the navigation bar
Private $ _ nextPageString = ">"; // the character displayed on the first page of the navigation bar
Private $ _ previousPageString = "<"; // character displayed on the last page of the navigation bar
Private $ _ lastPageString = ">>|"; // character displayed on the last page of the navigation bar
Private $ _ splitString = "|"; // delimiter between the numbers of words

Public function _ construct ($ itemCount, $ pageSize)
{
If (! Is_numeric ($ itemCount) | (! Is_numeric ($ pageSize ))){
Throw new Exception ("Pagination Error: not Number ");
}
$ This-> _ itemCount = $ itemCount;
$ This-> _ pageSize = $ pageSize;
$ This-> _ front = Zend_Controller_Front: getInstance ();

$ This-> _ pageCount = ceil ($ itemCount/$ pageSize); // total number of pages
$ Page = $ this-> _ front-> getRequest ()-> getParam ($ this-> _ PageParaName );

If (empty ($ page) | (! Is_numeric ($ page ))){
// Set the current page to 1 if it is null or not a number
$ This-> _ currentPage = 1;
} Else {
If ($ page <1 ){
$ Page = 1;
}
If ($ page> $ this-> _ pageCount ){
$ Page = $ this-> _ pageCount;
}
$ This-> _ currentPage = $ page;
}
}

Public function getCurrentPage ()
{
Return $ this-> _ currentPage;
}

Public function getNavigation ()
{
$ Navigation ='

';

// The number of tabs on the current page
$ PageCote = ceil ($ this-> _ currentPage/($ this-> _ navigationItemCount-1)-1;
// Total pagination bar
$ PageCoteCount = ceil ($ this-> _ pageCount/($ this-> _ navigationItemCount-1 ));
// Start Page in the pagination bar
$ PageStart = $ pageCote * ($ this-> _ navigationItemCount-1) + 1;
// Termination page in the paging bar
$ PageEnd = $ pageStart + $ this-> _ navigationItemCount-1;

If ($ this-> _ pageCount <$ pageEnd ){
$ PageEnd = $ this-> _ pageCount;
}

$ Navigation. = "total: {$ this-> _ itemCount} total {$ this-> _ pageCount} Page \ n ";

If ($ pageCote> 0) {// homepage navigation
$ Navigation. = 'createhref (1)
. "\" = ""> $ This-> _ firstPageString ";
}
If ($ this-> _ currentPage! = 1) {// previous navigation
$ Navigation. = 'createhref ($ this-> _ currentPage-1 );
$ Navigation. = "\" = ""> $ this-> _ previousPageString ";
} Else {
$ Navigation. = $ this-> _ previousPageString .'';
}

While ($ pageStart <= $ pageEnd) // Construct a digital navigation area
{
If ($ pageStart ==$ this-> _ currentPage ){
$ Navigation. ="$ PageStart". $ This-> _ splitString;
} Else {
$ Navigation. = 'createhref ($ pageStart)
. "\" = ""> $ PageStart"
. $ This-> _ splitString;
}
$ PageStart ++;
}

If ($ this-> _ currentPage! = $ This-> _ pageCount) {// next page navigation
$ Navigation. = '. $ this-> createHref ($ this-> _ currentPage + 1)
. "\" = ""> $ This-> _ nextPageString ";
} Else {
$ Navigation. = $ this-> _ nextPageString;
}

If ($ pageCote <$ pageCoteCount-1) {// non-page navigation
$ Navigation. = '. $ this-> createHref ($ this-> _ pageCount)
. "\" = ""> $ This-> _ lastPageString ";
}

$ Navigation. =''; For ($ I = 1; $ I <= $ this-> _ pageCount; $ I ++ ){If ($ this-> getCurrentPage () ==$ I ){$ Selected = "selected ";} Else {$ Selected = "";}$ Navigation. =''. $ I.'';}$ Navigation. ='';
$ Navigation. = "page

";
Return $ navigation;
}

Public function getNavigationItemCount ()
{
Return $ this-> _ navigationItemCount;
}

Public function setNavigationItemCoun ($ navigationCount)
{
If (is_numeric ($ navigationCount )){
$ This-> _ navigationItemCount = $ navigationCount;
}
}

Public function setFirstPageString ($ firstPageString)
{
$ This-> _ firstPageString = $ firstPageString;
}

Public function setPreviousPageString ($ previousPageString)
{
$ This-> _ previousPageString = $ previousPageString;
}

Public function setNextPageString ($ nextPageString)
{
$ This-> _ nextPageString = $ nextPageString;
}

Public function setLastPageString ($ lastPageString)
{
$ This-> _ lastPageString = $ lastPageString;
}

Public function setAlign ($ align)
{
$ Align = strtolower ($ align );
If ($ align = "center "){
$ This-> _ align = "center ";
} Elseif ($ align = "right "){
$ This-> _ align = "right ";
} Else {
$ This-> _ align = "left ";
}
}

Public function setPageParamName ($ pageParamName)
{
$ This-> _ PageParaName = $ pageParamName;
}

Public function getPageParamName ()
{
Return $ this-> _ PageParaName;
}

Private function createHref ($ targetPage = null)
{
$ Params = $ this-> _ front-> getRequest ()-> getParams ();
$ Module = $ params ["module"];
$ Controller = $ params ["controller"];
$ Action = $ params ["action"];

$ TargetUrl = $ this-> _ front-> getBaseUrl ()
. "/$ Module/$ controller/$ action ";

Foreach ($ params as $ key => $ value)
{
If ($ key! = "Controller" & $ key! = "Module"
& $ Key! = "Action" & $ key! = $ This-> _ PageParaName ){
$ TargetUrl. = "/$ key/$ value ";
}
}
If (isset ($ targetPage) {// specify the target page
$ TargetUrl. = "/$ this-> _ PageParaName/$ targetPage ";
} Else {
$ TargetUrl. = "/$ this-> _ PageParaName /";
}
Return $ targetUrl;
}
}

Here is a brief review of Mysql's limit offset

Assume that 13 data entries exist in the database table blog.

Statement 1: select * from blog limit 9, 4
Statement 2: select * from blog limit 4 offset 9

// Statements 1 and 2 both return the 10th, 11, 12, and 13 rows of the table blog.
// 9 in statement 1 indicates that four rows are returned starting from the tenth row of the table.
// 4 in statement 2 indicates 4 rows are returned, and offset 9 indicates starting from the tenth row of the table.

The following statement displays the paging effect:

Statement 3: select * from blog limit ($ this-> _ curPage-1) * self: PERPAGENUM, self: PERPAGENUM;
Statement 4: select * from blog limit self: PERPAGENUM offset ($ this-> _ curPage-1) * self: PERPAGENUM;

The specific effect of the Zend Framework paging class can be seen on the page effect of this site. the CSS style can be changed according to the personal design sense. Here I will give an example...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.