Implementation of smarty Paging

Source: Internet
Author: User
There are many methods for paging in smarty.
1. Use the pagination plug-in of smarty, such as pager, pagnition, and sliding_page. However, these plug-ins are not very good, and almost all of them have bugs.
If you are interested in trial and improvement, you can see:
Http://smarty.php.net/contribs/plugins/view.php/function.pager.php
Http://www.phpinsider.com/smarty-forum/viewtopic.php? T = 2327
Http://www.phpinsider.com/smarty-forum/viewtopic.php? T = 1604

2. When using the paging class library, there are too many online libraries, but I have not found it well written, especially easy to expand.
When searching, I also saw a terminator called the paging class. Haha, it's a bit funny. SQL is included in the paging class, which is absolutely intolerable. It can be said that the author's understanding of OO is still relatively shallow.

However, the paging class library is not necessarily suitable for smarty, especially when the record set data is a two-dimensional array, I don't want to give up the convenient section of smarty. This is why I didn't use pear: pager. Otherwise, it would be very troublesome to process the array first.

3. Write it by yourself .. I have not selected to write a class library for the time being, but it is easy to rewrite it. The following describes the ideas.

In fact, the page of smarty is very simple. First, we can implement it in the template as follows:
{$ Pager_links}
{Section name = "list" loop = $ productid start = 0 max = $ pager_total step = 1}
{If ($ smarty. Section. List. Index >=$ pager_startnum) & ($ smarty. Section. List. index <= $ pager_endnum )}
Product Name: {$ productname [LIST]}
Product Category: {$ catalogname [LIST]}
{/If}
{/Section}

The above can easily print a record set (two-dimensional array) and limit the display range of each page.
{$ Pager_links} page tag (that is, the previous page, next page, etc)
Max = $ pager_total total number of records
$ Smarty. Section. List. Index >=$ pager_startnum) & ($ smarty. Section. List. index <= $ pager_endnum)
This row is used to limit the display range of the record. If the index of the record falls within this range, it will be displayed; otherwise, it will not be displayed.

The above shows that in the PHP file, we only need to pass four variables to the smarty object:
1. Total number of records
2. Start number of records per page
3. Number of records on each page
4. Paging labels

Refer to the following code:
Labels can be written by yourself and expanded to more powerful ones. Now I don't have time, or I will write
<? PHP
$ Smartyarr = $ smarty-> get_template_vars ();

// Total number of records, number of records per page, total number of pages
$ Pager_total = count ($ smartyarr ['produd d']);
$ Pager_size = 10;
$ Pager_number = Ceil ($ pager_total/$ pager_size );
$ Pager_url = "index. php? Action = view ";

// The number of pages on the current page, obtained from the request
If (isset ($ _ Get ['pager _ pageid']) &! Empty ($ _ Get ['pager _ pageid']) {
$ Pager_pageid = intval ($ _ Get ['pager _ pageid']);
} Else {
// The first access
$ Pager_pageid = 1;
}

// Number of records starting from each page
If ($ pager_pageid = 1 ){
$ Pager_startnum = 0;
} Else {
$ Pager_startnum = ($ pager_pageid-1) * $ pager_size;
}

$ Pager_endnum = $ pager_startnum + $ pager_size;

If ($ pager_pageid = 1 & $ pager_number> 1 ){
// Page 1
$ Pager_links = "Previous Page | <a href =". $ pager_url. "& pager_pageid =". ($ pager_pageid + 1). "> next page </a> ";
} Elseif ($ pager_pageid = $ pager_number & $ pager_number> 1 ){
// Last page
$ Pager_links = "<a href =". $ pager_url. "& pager_pageid =". ($ pager_PageID-1). "> previous page </a> | next page ";
} Elseif ($ pager_pageid> 1 & $ pager_pageid <= $ pager_number ){
// Intermediate
$ Pager_links = "<a href = ". $ pager_url. "& pager_pageid = ". ($ pager_PageID-1 ). "> previous page </a> | <a href = ". $ pager_url. "& pager_pageid = ". ($ pager_pageid + 1 ). "> next page </a> ";
} Else {
$ Pager_links = "Previous Page | next page ";
}

$ Smarty-> assign ('pager _ total', $ pager_total );
$ Smarty-> assign ('pager _ startnum', $ pager_startnum );
$ Smarty-> assign ('pager _ endnum', $ pager_endnum );
$ Smarty-> assign ('pager _ links ', $ pager_links );

Return $ smarty-> fetch ("list. TPL ");
?>


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.