Page display detailed

xiaoxiao2021-03-06  181

Page display detailed

Author: owl Source: Beyond PHP 1, Introduction Pagination is a very common and display large amounts of data browsing method is one of the most common web programming event processing. 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 | | ';} // 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, The database connection in the OO style code is to process the PEAR DB class using the PEAR DB class for processing _ setOptions ($ OPTION); // Total number if (! isset ($ this-> numberms)) {$ RES = $ db-> query ($ this-> sql); $ this-> NumItems = $ RES -> Numrows ();} // 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 $ tris->

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;}} (! $ this-> isfirstpage) {$ this-> PreviousPageId = $ this-> currentpageID - 1;} } Return True;} / *** * * 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 expenses are not Very big, you can use the getPageData to get the result of the 2D array format * getPageData method is also the result of calling this method to get the result. * *** / function getDataLink () {if ($ this-> NumItems) {Global $ db; $ PageID = $ this-> currentpageId; $ from = ($ pageid - 1) * $ this-> Pagesize; $ count = $ this-> Pagesize; $ link = $ db-> limited ($ this-> sql, $ From, $ count); // Use the PEAR DB :: LimitQuery method to ensure database compatibility Return $ link;}}} / *** * * Return results set in the format of the two-dimensional array * *** / function getPageData () {ix ($ this-> NumItems) {if ($ RES = $ this-> getDatateLink ()) {if ($ rows-> nuMrows ()) {while ($ row = $ r) {$ 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;}}}}} > $ SQL, "PageSize" => 10, "currentPageID" => $ page); if (isset ($ _ get [number ") {$ Pager_OPTION ['NumItems'] = Int $ _ get ['NumItems']; Page | ";} else {$ turnover =" Previous page |";} f ($ pager-> islastpage) {$ turnover. = "Next | Last" ;

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

New Post(0)