PHP paging display make detailed explanation (transfer)

xiaoxiao2021-03-06  88

PHP paging display make detailed explanation

Author: Unknown Updated: Legume Source: ddvip.net Update Time: 2004.08.21 Contributor mail: tg * ddvip.com 1, Introduction Pagination is a very common browsers display large amounts of data and methods, belong to the most web programming One of the normal processed events. 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, the so-called paging display, that is, divide the result of the result group in the database to display, here you need 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 Record: SELECT * FROM TABLE LIMIT 20, 10 ...... this group of SQL statements In fact, when $ upesize = 10 Take the SQL statement of each page of the data, we can summarize such a template: SELECT * from Table Limit ($ CURRENTPAGEID - 1) * $ PAGESIZE, $ PageSize takes this template into the corresponding The value and the set of SQL statements on the upper side are looking at it. 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.

First Page | |';} IF (($ PAGE == $ Page_count) || ($ Page_count == 0)) { $ Page_String. = 'Next page | Last ";} else {$ Page_String. =' Next Page | Tail ';} // Get data, return the result in two-dimensional array format IF ($ AMOUNT) {$ SQL = "Select * from table order by id desc limit". ($ page-1) * $ page_size. ", $ page_size"; $ = = mysql_query ($ SQL); WHILE ($ ROW = mysql_fetch_row ($ result)) {$ rowset [] = $ row;}} else {$ rowset = array ();} // does not contain code that display results, that is not discussed, as long as itach can be very simple Use the obtained two-dimensional array to display the result?>

4, OO style code The following code The following code is to process the PEAR DB class for processing _ setoptions ($ Option); // Total number if (! isset ($ this-> numberms)) {$ res = $ db-> query ($ this-> sql); $ this-> NumItems = $ RES-> Numrows ();} // Total page number IF ($ this-> NumItems> 0) {if ($ this-> NumItems <$ this-> pagesize) {$ this-> Numpages = 1;} ($ 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; 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;} if ($ THIS- > Numpages> 1) {IF ($ this-> islastpage) {$ this-> NextPageId = $ this-> currentPageId 1;} if (! $ this-> isfirstpage) {$ this-> previouspageId = $ this- > CURRENTPAGEID - 1;}}}}} / *** * * Database connection for returning result set * You can use this method directly to get database connections when the result set is relatively large, then traverside outside the class, which overhead Small * If the result set is not very big,

You can use the getPageData to get the result of the 2D array format * getPageData method is also a result of calling this method to get the result. * *** / function getDataLink () {if ($ this-> NumItems) {Global $ db; $ pageID = $..-> CurrentPageId; $ from = ($ pageid - 1) * $ this-> Pagesize; $ count = $ this-> Pagesize; $ link = $ db-> limited query ($ this-> SQL, $ from, $ Count); // Using the Pear DB :: LimitQuery method to ensure database compatibility Return $ link;}} / *** * * Return results set in the format of the two-dimensional array * *** / function GetPageData () {IF ($ RES = $ this-> getDataLink ()) {if ($ r-> nuMrows ()) {while ($ row = $ r-> fetchrow ()) {$ Result [] = $}} else {$ result = array ();} Return $ Result;}}}}}}}}} function _SetOptions ($ OPTION) {$ allow_options = Array ('Pagesize', 'CurrentPageID', 'SQL'); Foreach ($ OPTION $ Key => $ VALUE) {IF (IN_Array ($ Key, $ ALLOW_OPTIONS) && ($ VALUE! = null) ) {$ this -> $ key = $ value;}}}}}?> $ SQL," Pagesize "=> 10," CurrentPageID "=> $ Page); if (isset ($ _ get [NumItems ')) {$ Pager_Option [' Numitems'] = (int) $ _ get ['NumItems'];} $ facer = @new pager ($ PAGER_OPTION); $ data = $ facer-> getPageData (); if ($ pager-> isfirstpage) {$ Turnover = "Home | Previous |"; Else {$ TurnOver = "


New Post(0)