PHP uses the Mysqli class library to achieve the perfect paging effect method _php skill

Source: Internet
Author: User
Tags add time php database php foreach

This article illustrates how PHP uses the Mysqli class library to achieve a perfect paging effect. Share to everyone for your reference, specific as follows:

This article is based on my last article "PHP Database operations based on MYSQLI database Operation class Library" and tailored, how to use the M class library Fetchall method to make perfect pagination.

Paging is essential in each of our projects and is very often present. This requires US programmers in the project how to the fastest speed, the most concise code to achieve the paging scheme.

The implementation of pagination is mostly based on the parameters passed in by the URL (typically page), such as: http://localhost/article.php?page=2 to take the second page of data

Recommendation: When you look at this article, please make sure that you have learned my last article "PHP database operation based on MYSQLI database Operation class Library"

Below we follow the M class library for pagination, the code appearing in the blog, and finally with the download address, including test database files.

1, establish the configuration file config.inc.php

The code listing is as follows

<?php
header (' content-type:text/html; Charset=utf-8 '); Set Header encoding
define (' Root_path ', DirName (__file__));//Set root
define (' db_host ', ' localhost ');//Database server address
define (' Db_user ', ' root ');//Database username
define (' db_pwd ', ' xxx ');//Database password, please fill in the
define (' db_name ', ' xxx ') according to the machine; Database name, please fill in the
define (' Db_port ', ' 3306 ') according to the machine,//Database port, please fill in
function __autoload ($className) {require_ by machine
  Once Root_path. '/includes/'. Ucfirst ($className). Class.php '; Automatically load class library file
}
?>

2, the establishment of information test documents article.php

Note: Because I have limited CSS ability, so in order to demonstrate the function, only the use of simple HTML
Code listings and comments are as follows

<?php require ' config.inc.php '; Introducing a configuration file $m = new M (); Instantiate M class $total = $m->total (' jzy_article '); Total news articles $page = new page ($total, 20);
Instantiate pagination class/* Note: 1. When the page class is paginated, two parameters need to be passed: Total number of records, number of records per page. 2. When the parameters are passed in, a Setlimit () method in the Page class automatically calculates the limit value in the SQL.
For example: When the URL parameter page is 1, the limit value is "0,20", and the limit value is "20,20" when it is 2. 3, the calculated $page->limit, must be placed in the Fetchall method of the last one, please check the Fetchall method * * * $data = $m->fetchall ("Jzy_article", "title, source , writer, pubdate "," "," "ID DESC", $page->limit);  According to the Fetchall method in M class library to get Data?> <style>/* Pagination Style * * * * #page {text-align:right; Padding:10px;clear:both;} #page a {border:1px solid #666;p adding: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;p adding: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= "> <th width=" 483 "> Title </th> <th width=" "> Source </th> <th width=" 15 4 "> Author </th> <th width=" 194 > Add time </th> </tr> <?php foreach ($data as $v) {//Loop out data?> &L T;tr> <td> <?php echo $v [' title '];?></td> <td align= "center" ><?php echo $v [' source ']; ?></td> <td align= "center" ><?php Echo $v [' writer '];?></td> <td "center" align=;? PHP echo $v [' pubdate ']?></td> </tr> <?php}?> <tr> <td id= "page" colspan= "4" ><?ph

 P echo $page->fpage ();?></td> <!--pull up paging class--> </tr> </table>

3. Access to test results

Open the browser, enter the test URL address, your browser should have the following effects

More interested in PHP related content readers can view the site topics: "Php Curl Usage Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document skills Summary (including Word, Excel,access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.