Web development is the mainstream of distributed programming development in the future, the common web development involves dealing with the database, the client reads from the server side is usually in the form of pagination to display, one page of reading up is both convenient and beautiful. So the write paging program is an important part of web development, here, we together to study the preparation of the paging program.
the principle of the paging procedure
The paging program has two very important parameters: each page displays several records ($pagesize) and is currently the first page ($page). With these two parameters can be very convenient to write the paging program, we use the MySQL database as a data source, in MySQL if you want to remove a paragraph in the table specific content can use the T-SQL statement: SELECT * FROM table Limit offset,rows to achieve. The offset here is the record deviation, which is calculated by offset= $pagesize * ($page-1), rows is the number of records to display, and this is $page. That is, select * FROM table limit 10,10 The meaning of this statement is to remove 20 records starting from the 11th record.
Second, the main code analysis
$pagesize = 10; Set the number of records to display per page $conn=mysql_connect ("localhost", "root", ""); Connection Database $rs=mysql_query ("SELECT count (*) from tb_product", $conn); Total Records $rs$myrow = Mysql_fetch_array ($rs); $numrows = $myrow [0];//calculates the total number of pages $pages=intval ($numrows/$pagesize); Determine the page setting if (Isset ($_get[' page ')) {$page =intval ($_get[' page ')); else{$page = 1;//Otherwise, set to first page}
|
Third, create use cases 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, complete code
v. Summary
This example code runs normally on the Windows2000 server+php4.4.0+mysql5.0.16. The example shows the paging format as [1][2][3] ... Such a form. If you want to display "first prev next last" form, please add the following code:
$first =1 $prev = $page-1; $next = $page +1; $last = $pages; if ($page > 1) {echo "
homepage "; echo "
previous page ;} if ($page $pages) {echo "
next page echo "
last ;}
|
In fact, the write pagination display code is very simple, as long as mastered its working principle. Hopefully this article will be able to bring to those who need this program Web Programmer's Help.