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.