How to write PHP paging code

Source: Internet
Author: User
First, the principle of the paging program
The paging program has two very important parameters: Each page shows several records ($pagesize) and the current page ($page). With these two parameters can be very convenient to write out the paging program, we use the MySQL database as a data source, in MySQL if you want to remove a particular piece of the table can be used in a certain content of T-SQL statement: SELECT * FROM table limit offset,rows to implement. Offset here is the record offset, it is calculated by offset= $pagesize * ($page-1), rows is the number of records to display, here is $page. That is to say, select * FROM table limit 10,10 This statement means to take out the table from the 11th record starting from the 20 records.

Second, the main code parsing

$pagesize = 10; Set the number of records displayed per page $conn=mysql_connect ("localhost", "root", ""); Connect Database $rs=mysql_query ("SELECT count (*) from tb_product", $conn); Total records obtained $rs$myrow = Mysql_fetch_array ($rs); $numrows = $myrow [0];//calculates the total number of pages $pages=intval ($numrows/$pagesize);// Determine the number of pages set if (Isset ($_get[' page ')) {$page =intval ($_get[' page '));} else{$page = 1;//Otherwise, set to first page}

Three, create use case with Table myTable

CREATE TABLE myTable (id int not NULL auto_increment,news_title varchar (), News_cont text, Add_time datetime,primary KEY (ID))

Four, full code

V. Summary

This example code runs normally on Windows2000 server+php4.4.0+mysql5.0.16. The page format shown by the example is [1][2][3] ... Such a form. If you want to display the "first page UP next page" In this form, please add the following code:

$first =1; $prev = $page-1; $next = $page +1; $last = $pages; if ($page > 1) {echo " previous page  ";} if ($page $pages) {echo " last  ";}

Web development is the mainstream of distributed programming in the future, usually the web development involves dealing with the database, the client from the server-side reading is usually in the form of paging, page by page reading is both convenient and beautiful. So to write a paging program is an important part of web development.

Related Article

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.