Page display detailed

xiaoxiao2021-03-05  57

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.

| ';} // Get data, return the result if ($ AMOUNT) {$ SQL = in two-dimensional array format "Select * from table order by id desc limited". ($ page-1) * $ page_size "; $ results = mysql_query ($ sql); while ($ row = mysql_fetch_row ($ result) {$ Rowset [] =

$ row}} else {$ rowset = array ();} // does not contain code that the display result is not discussed, as long as you can use foreach to display the results very simple to display the result?> 4, OO style code The following code is processed using the PEAR DB class.

_ setOptions ($ Option); // Total number IF (! Isset) ($ RES = $ db-> query ($ this-> sql); $ this-> numitems = $} ($ THIS)} // Total number IF ($ this- > NumItems> 0) {IF ($ this-> Numitems <$ this-> pagesize) {$ this-> Numpages = 1;} if ($ this-> NumItems% $ this-> pagesize) {$ this-> Numpages = (int) ($ this-> NumItems / $ this-> pagesize) 1;} else {$ this-> Numpages = $ this-> NumItems / $ this-> Pagesize;}} else {$ this-> Numpages = 0;} Switch ($ this-> currentpageid) {Case $ this-> Numpages == 1: $ this-> isfirstpage = true; $ this-> IslastPage = True; Break; Case 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 connection * in the result When the set is bigger, you can use this method to get the database connection, then traverse it outside the class, so that the overhead is smaller * If the result set is not very large, you can directly use the getPageData to get the result of the two-dimensional array format * getPageData method is also Calling this method to get results * *** / function getDataLink () {if ($ this-> NumItems) {Global $ db; $ pageID; $ from = ($ pageID - 1) * $ this-> PageSize; $ count = $ this-> pagesize; $ link = $ db-> limited; $ 10); // Using PEAR DB :: LimitQuery method to ensure database compatibility Return $ link;} else {return false;}} / *** * * with two-dimensional array format Return Results * *** / Function getPageData () {if ($ this-> NumItems) {if ($ res = $ this-> getDataLink ()) {if ($ res-> nuMrows ()) {while ($ Row = $ res-> fetchrow ()) {$ result [] = $ row;}} else {$ result = array ();} Return $ Result;} else {returnaf

}}}}} Function _SETOPTIONS ($ OPTION) {$ allow_Options = array ('pagesize', 'currentpageid', 'sql', 'numberms'); Foreach ($ OPTION $ Key => $ VALUE) {IF (IN_ARRAY ($ Key, $ Allow_Options && ($ Value! = NULL) {$ this -> $ key = $ value;}}}}}

$ SQL, "PageSize" => 10, "currentpageid" => $ page); if (isset ($ _ get [number ") {$ Pager_OPTION ['NumItems'] = (int) $ _ get [' NumItems '];} $ facer = @new pager ($ PAGER_OPTION); $ data = $ pager-> getPageData (); if ($ pager-> isfirstpage) {$ turnover = "Home | Previous |; $ turnover = " |";} if ($ pager-> islastpage) {$ turnover. = "Next | Last";} else {$ Turnover. = " Thain ";}?> 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:

getPageData (); // Display code code // ...}} /// Call IF (iSset ($ _GET ['Page']) {$ Page = (int) $ _ get ['Page'];} $ SQL = "SELECT * from Members Order By ID"; $ Pager_Option = Array ("SQL" => $ SQL, "PageSize" => 10, "CurrentPageID" => $ page); if (isset ($ _ get [n items')) {$ Pager_Option ['NumItems'] = (int $ _ Get ['Numitems'];} $ pacer = @new memberpager ($ pager_option); $ pager-> showmemberlist ();?> The second thing to explain is the compatibility of different databases, in different databases The way to intercept a result is different. MySQL: SELECT * FROM TABLE LIMIT OFFSET, ROWSPGSQL: Select * from Table Limit M Offset N ... So you want to use the PEAR DB class's LimitQuery method when getting the result.

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

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

New Post(0)