Detailed explanations on the production of paging display in PHP programming

Source: Internet
Author: User
* Database connection to the returned result set * When the result set is large, you can directly use this method to obtain the database connection and traverse outside the class, this reduces the overhead. * If the result set is not large, you can directly use getPageData to obtain the results in two-dimensional array format. * The getPageData method also calls this method to obtain the results ****

* Database connection to the returned result set * When the result set is large, you can directly use this method to obtain the database connection and traverse outside the class, this reduces the overhead. * If the result set is not large, you can directly use getPageData to obtain the results in two-dimensional array format. * The getPageData method also calls this method to obtain the results ****/

* Database connection to the returned result set

* When the result set is large, you can directly use this method to obtain the database connection and traverse the database outside of the class. This reduces the overhead.

* If the result set is not large, you can directly use getPageData to obtain results in two-dimensional array format.

* The getPageData method also calls this method to obtain the result.

*

***/

Function getDataLink ()

{

If ($ this-> numItems)

{

Global $ db;

$ PageID = $ this-> CurrentPageID;

$ From = ($ PageID-1) * $ this-> PageSize;

$ Count = $ this-> PageSize;

$ Link = $ db-> limitQuery ($ this-> SQL, $ from, $ count); // use the Pear DB: limitQuery method to ensure database compatibility

Return $ link;

}

Else

{

Return false;

}

}

/***

*

* Returns the result set in two-dimensional array format.

*

***/

Function getPageData ()

{

If ($ this-> numItems)

{

If ($ res = $ this-> getDataLink ())

{

If ($ res-> numRows ())

{

While ($ row = $ res-> fetchRow ())

{

$ Result [] = $ row;

}

}

Else

{

$ Result = array ();

}

Return $ result;

}

Else

{

Return false;

}

}

Else

{

Return false;

}

}

Function _ setOptions ($ option)

{

$ Allow_options = array (

'Pagesize ',

'Currentpageid ',

'SQL ',

'Numitems'

);

Foreach ($ option as $ key => $ value)

{

If (in_array ($ key, $ allow_options) & ($ value! = Null ))

{

$ This-> $ key = $ value;

}

}

Return true;

}

}

?>

// FileName: test_pager.php

// This is a simple sample code. The code for establishing a database connection using the pear db class is omitted in the front.

Require "Pager. class. php ";

If (isset ($ _ GET ['page'])

{

$ Page = (int) $ _ GET ['page'];

}

Else

{

$ Page = 1;

}

$ SQL = "select * from table order by id ";

$ Pager_option = array (

"SQL" => $ SQL,

"PageSize" => 10,

"CurrentPageID" => $ page

);

If (isset ($ _ GET ['numitems '])

{

$ Pager_option ['numitems '] = (int) $ _ GET ['numitems'];

}

$ Pager = @ new Pager ($ pager_option );

$ Data = $ pager-> getPageData ();

If ($ pager-> isFirstPage)

{

}

Else

{

}

If ($ pager-> isLastPage)

{

}

Else

{

}

?>

There are two areas to note:

This class only processes data and is not responsible for displaying the data, because I feel that it is a little effort to put the data processing and result display in a class. When the display status and requirements are changeable, it is better to handle the problem based on the results given by the class. The better way is to display different pages based on the Pager class inheriting a subclass of its own, for example, to display the user page list, you can:

Class MemberPager extends Pager

{

Function showMemberList ()

{

Global $ db;

$ Data = $ this-> getPageData ();

// Code for displaying the result

//......

}

}

/// Call

If (isset ($ _ GET ['page'])

{

$ Page = (int) $ _ GET ['page'];

}

Else

{

$ Page = 1;

}

$ SQL = "select * from members order by id ";

$ Pager_option = array (

"SQL" => $ SQL,

"PageSize" => 10,

"CurrentPageID" => $ page

);

If (isset ($ _ GET ['numitems '])

{

$ Pager_option ['numitems '] = (int) $ _ GET ['numitems'];

}

$ Pager = @ new MemberPager ($ pager_option );

$ Pager-> showMemberList ();

?>

The second thing to note is the compatibility of different databases. The way to intercept a piece of results in different databases is different.

Mysql: select * from table limit offset, rows

Pgsql: select * from table limit m offset n

......

Therefore, you must use the limitQuery method of the pear db class to obtain results in the class.

OK. You don't think it is a waste of time to finish reading these words.

[1] [2]

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.