PHP paging display make detailed explanation

xiaoxiao2021-03-05  61

1. Password Paging Display is a very common way to browse and display a lot of data, which is 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, 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. = ' | Tissue ';} // Get data, return the result if ($ AMOUNT) {$ SQL = "SELECT *" ($ PAGE-1) in two-dimensional array format. * $ page_size. ", $ page_size"; $ result = mysql_query ($ SQL); while ($ ROW = mysql_fetch_row ($ rowset) {$ 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 PEAR DB Class Treatment _ setOptions ($ Option); // Total number IF (! $ rs = $ db-> query ($ this-> sql); $ this-> NumItems = $} ();} // 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 = 0;} Switch ($ this-> currentpageid) {Case $ this-> Numpages == 1: $ this-> isfirstpage = true; $ this-> islastpage = true; break; cas 1 : $ This-> isfirstpage = true; $ this-> islastpage = false; break; casage $ this-> Numpages: $ this-> isfirstpage = false; $ this-> islastpage = true; break; default: $ this-> IsFirstPage = false; $ this-> islastpage = false;}} ($ this-> numpages>

1) {if (! $ This-> islastPage) {$ this-> nextpageId = $ this-> currentpageid 1;}}}}} {$ this-> prepVageid = $ this-> currentpageID - 1;}}}} / *** * * Return the result set database * You can use this method to get the database connection directly when the result set is bigger, then traverse it outside the class, so that the overhead is smaller * if The result set is not very large, can directly use the getPageData to obtain the result of the two-dimensional array format * getPageData method also call this method to get the result * *** / function getDataLink () {if ($ this-> NumItems) {global $ DB; $ pageID; $ from = ($ pageid - 1) * $ this-> pageesize; $ count = $ this-> pagesize; $ link = $ db-> limited query ($ this-> SQL, $ from, $ count); // Using the Pear DB :: LimitQuery method to ensure database compatibility Return $ link;}} / *** * * Return the result set in the format of the two-dimensional array * *** / function getPageData () {if ($ this-> NumItems) {if ($ res = $ this-> getDatateLink ()) {if ($ r-> nuMrows ()) {while ($ row = $ RES -> fetchrow ()) {$ result [] = $ row}} else {$ result = array ();} 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;}}} Return True;

}}?> $ sql, "pagesize" => 10, "currentPageID" => $ page); if (isset ($ _ get [n items')) {$ Pager_OPTION ['NumItems'] = (int) $ _Get ['NumItems'];} $ Pager = @new Pager ($ PAGER_OPTION); $ data = $ facer-> getPageData (); if ($ pager-> isfirstpage) {$ turnover = "Home | Previous |" } else {$ turnover = " Previous |";} f ($ pager-> islastpage) {$ turnover. = "Next | Last";} else {$ Turnover. = " Numpages. "& numItems =". $ pacer-> numItems. "'> Tissue ";}?> There are two places that need to be explained: this class only processes data, and Not responsible for processing display, because I feel that the display of processing and results of the data is active in a class.

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

New Post(0)