Page Display Details

zhaozj2021-02-16  69

1 Introduction

Page Display is a very common way to browse and display large amounts of data, belonging to one of the events of the event in Web programming. For web programming, writing this code is just as natural as breathing, but for beginners, often can't touch this problem, so specially writing this article for detailed explanation, seeking to see Friends in this article have been understood after reading the principles and implementation methods of paging display. This article is suitable for beginners to read, all sample code is written in PHP.

2, principle

The so-called paging display is to display the result of the result in the database into one segment, which requires two initial parameters:

How many records per page ($ PAGESIZE)? Is the current page ($ currentPAGEID)?

Just give me a result set now, I can display a specific result of a certain paragraph. As for other parameters, such as: Previous ($ PreviousPageID), next page ($ NextPageID), total number of pages ($ Numpages), etc., can be obtained according to these things in front. Take the mysql database as an example. If you want to intercept a certain segment from the table, the SQL statement can be used: SELECT * from Table Limit Offset, Rows. See a set of SQL statements below, try discovering the rates.

Top 10 Records: SELECT * FROM TABLE LIMIT 0, 10 Articles 11 to 20 Records: SELECT * FROM TABLE LIMIT 10, 10 Articles 21 to 30 Records: SELECT * FROM TABLE LIMIT 20, 10 ......

This set of SQL statements is actually a SQL statement for each page of the data when $ PAGESIZE = 10, we can summarize such a template:

Select * from table limit ($ CurrentPageID - 1) * $ PAGESIZE, $ PAGESIZE

Take this template into the corresponding value and the set of SQL statements in the upper side, see if it is so going. After gaining the most important problem, the remaining is just the transfer parameters, construct the appropriate SQL statement and then use PHP to obtain data from the database and display. I will explain with specific code.

3, simple code, please read the following code in detail, you will debug it once, it is best to modify it once, plus your own features, such as search, and so on.

Code:

// Establish a database connection $ link = mysql_connect ("localhost", "mysql_user", "mysql_password") or Die ("COULD NOT Connect:". MySQL_ERROR ());

// Get the current page number if (isset ($ _ get ['Page']) {$ Page = INTVAL ($ _GET ['Page']);} else {$ Page = 1;}

/ / The number per page is $ Pagesize = 10;

// Get the total amount of data $ SQL = "Select count (*) as amount from table; $ r = mysql_query ($ SQL); $ row = mysql_fetch_row ($ result); $ AMOUNT = $ ROW ['Amount'];

// How many pages of pages IF ($ AMOUNT) {IF ($ AMOUNT) {IF ($ AMOUNT = 1;} // If the total amount of data is less than $ PAGESize, only one page IF ($ AMOUNT% $ Page_SIZE ) {// Take the total amount of data by the remaining number of each page $ Page_Count = ($ AMOUNT / $ PAGE_SIZE) 1; // If there is a rest number, the number of pages is equal to the total number of data divided by the number of per page. Results Adjusted plus 1} else {$ page_count = $ apou {page_size; // If there is no rest, the number of pages is equal to the total number of data divided by the number of per page}} else {$ Page_Count = 0;} // Turn link $ Page_String = ''; if ($ page == 1) {$ Page_String. = 'First Page | Previous |';} else {$ Page_String. = 'First Page | Previous |' }

IF (($ page == $ page_count) || ($ page_count == 0)) {$ Page_String. = 'Next Page | Last ";} else {$ Page_String. =' Next | Thain '; }

// Get data, return the result in two-dimensional array format if ($ AMOUNT) {$ SQL = "SELECT *." ($ Page-1) * $ page_size. ", $ Page_Size"; $ Result = mysql_query ($ sql); while ($ ROW = mysql_fetch_row ($ RESULT)) {$ rowset [] = $ row;}} else {$ rowset = array ();}

/ / Does not include the code of the display result, that is not discussed, as long as you can use foreach to display the result of the resulting two-dimensional array?> 4, OO style code The following code is used by the PEAR DB class Process

Code:

// filename: Pager.class.php // Patement class, this class is only used to process data structures, not responsible for processing displayed

Class Pager {var $ pagesize; // Var $ currentPageId; // Current page number $ nextpageId; // Next Var $ previousPageId; // Previous VAR $ Numpages; // Total number VAR $ NumItems; // Total number of VAR $ isfirstpage; // Is the first page var $ islastPage; // Whether the last page VAR $ sql; // SQL Query Statement Function Pager ($ OPTION) {Global $ DB; $ this -> _ setoptions ($ OPTION); // Total number if (! isset ($ this-> numberms)) {$ res = $ db-> query ($ this-> sql); $ this-> NumItems = $ $ THIS-> NumItems = $ Res-> Numrows ();} // Total pages IF ($ this-> Numitems> 0) {$ this-> Numpages = CEIL ($ this-> NumItems / $ this-> pagesize);} else {$ this -> Numpages = 0;} Switch ($ this-> currentpageid) {CASE $ this-> Numpages == 1: $ this-> isfirstpage = true; $ this-> islastpage = true; break; casse 1: $ this-> isfirstpage = true; $ this-> islastpage = false; break; case $ this-> Numpages: $ this-> isfirstpage = false; $ this-> IslastPage = true; break; default: $ this-> isfirstpage = false; $ this-> islastpage = false;} = f ($ this-> numpages> 1) {if (! $ this-> islastpage) {$ this-> NextPageId = $ this-> CurrentPageID 1;

} F (! $ This-> isfirstpage) {$ this-> PreviousPageId = $ this-> currentpageID - 1;}} Return True;} / *** * * Return the result set database * In the result set is bigger You can use this method to get database connections directly, then traversal outside of the class, which is smaller than the expenses. Get results * *** / function getDatateLink () {if ($ this-> numitems) {global $ db; $ pageid; $ from = ($ pageID - 1) * $ this-> Pagesize $ Count = $ this-> Pagesize; $ link = $ db-> limited Query ($ this-> SQL, $ from, $ count); // Using PEAR DB :: LimitQuery method to ensure database compatibility Return $ link;} Else {return false;}} / *** * * Return results set in the format of the two-dimensional array * *** / function getPageData () {if ($ this-> NumItems) {ified ($ res = $ this-> GetDataLink ()) { IF ($ RES-> Numrows ()) {While ($ row = $ r {$ result [] = $ row;}} else {$ results = array ();} returnome Else {Return False;}} else {return false;}} function _SETOPTIONS ($ OPTION) {$ allow_options = array ('Pagesize "

, 'Currentpageid', 'sql', 'Numitems'; Foreach ($ OPTION $ Key => $ VALUE) {IF (IN_ARRAY ($ Key, $ Allow_Options && ($ Value! = Null) {$ this- > $ key = $ value;}}}}}}}?> // filename: Test_pager.php // This is a simple sample code, omitting code using the PEAR DB class to establish database connection

Require "pager.class.php";

IF (isset ($ _ get ['Page']) {$ 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 ($ TURNOVER = "Home | Previous |";} else {$ turnover = "NumItems."> Home | PreviousPageId. "& numItems =". "' > Previous | ";

IF ($ Pager-> islastPage) {$ Turnover. = "Next | Last";} else {$ Turnover. = "NextPageId." & NumItems = "." "& numItems =". $ pager-> NumItems. "'> Last";}?> There are two places that need to be explained:

This class only processes data, is not responsible for processing display, because I think it is a bit boring to put the data processing and results. When the situation is displayed, the situation is changed, it is not as handled according to the results given by the class, and better ways to inherit a subclass according to this Pager class to display different pactions, such as displaying the user paging list:

Code:

Class MemberPager Extends Pager {Function Showmemberlist () {Global $ DB; $ DATA = $ this-> getPageData (); // Display code // ...}}

/// Call the IF (isset ($ _ get ['Page')) {$ Page '];} else {$ Page = 1;}

$ SQL = "SELECT * from mail" 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 need to explain is the compatibility of different databases, and the writing of a result is not the same in different databases. MySQL: SELECT * FROM TABLE LIMIT OFFSET, ROWS PGSQL: SELECT * FROM TABLE LIMIT M OFFSET N ... So you want to use the LimitQuery method of the PEAR DB class when getting the results.

OK, I have finished the post, I hope that you don't think it is a waste of time.

转载请注明原文地址:https://www.9cbs.com/read-14787.html

New Post(0)