Ec (2); Web development is the mainstream of distributed program development in the future. Generally, web development involves dealing with databases, and client reads from the server are usually displayed on pages, one-page reading is both convenient and beautiful. Therefore, writing paging programs is an important part of web development. Here, we will study the compilation of paging programs. I. Paging program principle paging program has two very important parameters: several records ($ pagesize) and the current page ($ page) are displayed on each page ). Script ec (2); script
Web development is the mainstream of distributed program development in the future. Generally, web development involves dealing with databases, and client reads from the server are usually displayed in pages, one-page reading is both convenient and beautiful. Therefore, writing paging programs is an important part of web development. Here, we will study the compilation of paging programs.
I. Principle of paging Program
The paging program has two very important parameters: several records ($ pagesize) are displayed on each page and the current page ($ page ). With these two parameters can be very convenient to write paging program, we take MySql database as the data source, in mysql if you want to retrieve a specific section of the table content can use the T-SQL statement: select * from table limit offset and rows. Here, offset is the record offset. Its calculation method is offset = $ pagesize * ($ page-1). rows is the number of records to be displayed. Here is $ page. That is to say, the select * from table limit 10, 10 statement means to retrieve 20 records starting from 11th records in the table.
Ii. Main Code Analysis
$ Pagesize = 10; // set the number of records displayed on each page
$ Conn = mysql_connect ("localhost", "root", ""); // connect to the 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 the total number of pages
$ Pages = intval ($ numrows/$ pagesize );
// Determine page number settings
If (isset ($ _ GET ['page']) {
$ Page = intval ($ _ GET ['page']);
}
Else {
$ Page = 1; // otherwise, set it to the first page.
}
Iii. Create a use case using myTable
Create table myTable (id int not null auto_increment, news_title varchar (50), news_cont text, add_time datetime, primary key (id ))
Iv. complete code
<Html>
<Head>
<Title> php paging example </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<? Php
$ Conn = mysql_connect ("localhost", "root ","");
// Set the number of records displayed on each page
$ Pagesize = 1;
Mysql_select_db ("mydata", $ conn );
// Get the total number of records $ rs, used to calculate the total number of pages
$ Rs = mysql_query ("select count (*) from tb_product", $ conn );
$ Myrow = mysql_fetch_array ($ rs );
$ Numrows = $ myrow [0];
// Calculate the total number of pages