PHP learning: Implementation of Smarty paging

Source: Internet
Author: User
There are many methods for paging in Smarty. 1. The pagination plug-ins of the application Smarty, such as Pager, pagnition, and sliding_page, are not very good, but almost all of them have bugs. There are hobbies to try and improve their own friends can look at: http://smarty.php.n Smarty pages in a lot of methods.
1. The pagination plug-ins of the application Smarty, such as Pager, pagnition, and sliding_page, are not very good, but almost all of them have bugs.
If you are interested in trial use 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. The application paging class library is too large on the Internet, but I have not invented it well, especially it is easy to expand.
When searching, I also saw a Terminator called the paging class. haha, it's a bit ridiculous. The SQL statements are included in the paging class. this is intolerable. it can be said that the author is quite familiar with OO.

However, the fire page 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 is an important idea.

Actually, 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 category 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 scope of the record. if the index of the record falls into this category, 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 themselves, and can be expanded to more powerful. Now I don't have time, or I will write
$ 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']) {

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.