Web development (which accounts for a large proportion in current program development) is the mainstream of distributed program development in the future. Generally, web development (which accounts for a large proportion in current program development) it involves dealing with databases. The client reads data from the server are usually displayed in pages. Reading one page is both convenient and beautiful. Therefore, writing paging programs is an important part of web development (which occupies a large proportion in current program 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, you can easily write a paging program. We use MySQL (the best combination with PHP) as the data source, and use MySQL (the best combination with PHP) if you want to retrieve a specific segment of the table, you can use the T-SQL statement: select * from table limit offset, rows to implement. 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 (the best combination with PHP) _ connect ("localhost", "root", ""); // connect to the database
$ Rs = MySQL (best combination with PHP) _ query ("select count (*) from tb_product", $ conn); // retrieve the total number of records $ rs
$ Myrow = MySQL (the best combination with PHP) _ 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, Prima (the most perfect VM management system) ry key (id ))
Iv. complete code
<Html>
<Head>
<Title> php (as the mainstream development language) Paging example </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<? Php (as the mainstream development language)
$ Conn = MySQL (the best combination with PHP) _ connect ("localhost", "root ","");
// Set the number of records displayed on each page
$ Pagesize = 1;
MySQL (best combination with PHP) _ select_db ("mydata", $ conn );
// Get the total number of records $ rs, used to calculate the total number of pages
$ Rs = MySQL (the best combination with PHP) _ query ("select count (*) from tb_product", $ conn );
$ Myrow = MySQL (the best combination with PHP) _ fetch_array ($ rs );
$ Numrows = $ myrow [0];
// Calculate the total number of pages
$ Pages = intval ($ numrows/$ pagesize );
If ($ numrows % $ pagesize)
$ Pages ++;
// Set the page number
If (isset ($ _ GET [page]) {
$ Page = intval ($ _ GET [page]);
}
Else {
// Set it to the first page
$ Page = 1;
}
// Calculate the record offset
$ Offset = $ pagesize * ($ page-1 );
// Read the specified number of records
$ Rs = MySQL (best combination with PHP) _ query ("select * from myTable order by id desc limit $ offset, $ pagesize", $ conn );