PHP uses the Mysqli class library to achieve the perfect paging effect, mysqli class library
This example describes how PHP uses the Mysqli class library to achieve the perfect paging effect. We will share this with you for your reference. The details are as follows:
This article is based on my previous article "PHP Database Operations Based on Mysqli database operation class library". How can I use the FetchAll method in the M class library to create a perfect page.
Paging is essential in each of our projects and is frequently used. This requires our programmers to implement paging solutions with the fastest speed and most concise code in the project.
Most paging implementations are implemented based on URL input parameters (usually page), such as http: // localhost/article. php? Page = 2 indicates the second page of data.
Suggestion: when reading this article, make sure that you have learned my previous article "mysql-based database operation class library for PHP database operations".
The following is a paging explanation based on the M class library. The code shown in the blog post is appended with the test database file.
1. Create the configuration file config. inc. php.
The code list is as follows:
<? Phpheader ('content-Type: text/html; Charset = UTF-8 '); // sets the header encoding define ('root _ path', dirname (_ FILE __)); // set the root directory define ('db _ host', 'localhost'); // the database server address define ('db _ user', 'root '); // database username define ('db _ pwd', '×'); // Database Password. Enter define ('db _ name ', '×××'); // database name. Enter define ('db _ port', '123') based on the machine; // database PORT, enter function _ autoload ($ className) {require_once ROOT_PATH. '/DES /'. ucfirst ($ className) . '. Class. php'; // automatically loads the class library file.}?>
2. Create an information test file article. php.
Note: Due to my limited CSS capabilities, only HTML is used for demonstration purposes.
The code list and comments are as follows:
<? Phprequire 'config. inc. php '; // introduce the configuration file $ m = new M (); // instantiate M class $ total = $ m-> Total ('jzy _ article '); // total number of news articles $ page = new Page ($ total, 20); // instantiate the paging class/* Note: 1. When the Page class is displayed, two parameters are required: the total number of records, and the number of records displayed on each page. 2. When a parameter is input, the setLimit () method in the Page class automatically calculates the limit value in the SQL statement. For example, when page is 1 in the URL parameter, the limit value is ""; When page is 2, the limit value is "20, 20 "...... 3. The calculated $ page-> limit must be placed in the last digit of the FetchAll method. For details, see the FetchAll Method */$ data = $ m-> FetchAll ("jzy_article ", "title, source, writer, pubdate", "", "id DESC", $ page-> limit); // obtain data based on the FetchAll method in the M class library?> <Style>/* Paging style */# page {text-align: right; padding: 10px; clear: both ;}# page a {border: 1px solid #666; padding: 2px 5px; margin: 0 2px; color: #3b6ea5; text-decoration: none ;}# page a: hover, # page span. me {color: # fff; border: 1px solid #000; background: #000; text-decoration: none ;}# page span. disabled {border: 1px solid # ccc; padding: 2px 5px; margin: 0 2px; color: # ccc ;}# page span. me {padding: 2px 5px; margin: 0 2px ;}</style> <Table width = "1000" border = "1" style = "border-collapse: collapse; font-size: 13px; "> <tr height =" 30 "> <th width =" 483 "> title </th> <th width =" 141 "> source </th> <th width = ""154"> author </th> <th width = "194"> Add time </th> </tr> <? Phpforeach ($ data as $ v) {// retrieve data cyclically?> <Tr> <td> <? Php echo $ v ['title'];?> </Td> <td align = "center"> <? Php echo $ v ['source'];?> </Td> <td align = "center"> <? Php echo $ v ['writer'];?> </Td> <td align = "center"> <? Php echo $ v ['pubdate'];?> </Td> </tr> <? Php }?> <Tr> <td id = "page" colspan = "4"> <? Php echo $ page-> fpage ();?> </Td> <! -- Call up the paging class --> </tr> </table>
3. Access test results
Open your browser and enter the url of the test. Your browser should have the following effect: