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 specific content can use the T-SQL statement: 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);
Determine 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
php pagination Example
$conn =mysql_connect (" localhost "," root "," ");
//Set the number of records displayed on each page
$pagesize = 1;
mysql_select_db ("MyData", $conn);
//Get total number of records $rs, calculate total pages by
$rs =mysql_query ("SELECT count (*) from tb_product", $conn);
$myrow = Mysql_fetch_array ($ RS);
$numrows = $myrow [0];
//Count total pages