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 section of the table can be used for a specific content of T-SQL statements: SELECT * FROM table limit offset,rows to implement. 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", ""); Connecting to a database
$rs =mysql_query ("SELECT count (*) from tb_product", $conn); Get the total number of records $rs
$myrow = Mysql_fetch_array ($RS);
$numrows = $myrow [0];
Calculate Total Pages
$pages =intval ($numrows/$pagesize);
Decision Page Setting
if (Isset ($_get[' page ')) {
$page =intval ($_get[' page '));
}
else{
$page = 1; Otherwise, set to the 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
<title>php Pagination Sample </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<?php
$conn =mysql_connect ("localhost", "root", "");
Set the number of records to display on each page
$pagesize = 1;
mysql_select_db ("MyData", $conn);
Get the total number of records $rs, calculate total pages with
$rs =mysql_query ("SELECT count (*) from tb_product", $conn);
$myrow = Mysql_fetch_array ($RS);
$numrows = $myrow [0];
Calculate Total Pages
$pages =intval ($numrows/$pagesize);
if ($numrows% $pagesize)
$pages;
Set Number of pages
if (Isset ($_get[' page ')) {
$page =intval ($_get[' page '));
}
else{
Set as first page
$page = 1;
}