Web development is the mainstream of distributed program development in the future. generally, web development involves dealing with databases, and client reading from the server is usually displayed on pages, one page is convenient and elegant. So write points
Web development is the mainstream of distributed program development in the future. generally, web development involves dealing with databases, and client reading from the server is usually displayed on pages, one page is convenient and elegant. 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, you can easily write the paging program.MySqlDatabase as the data source, inMySQLIf you want to extract a specific part of the table, you can apply the T-SQL statement: select * from table limit offset, rows to achieve. Here, offset is the record offset, and its calculation method is offset = $ pagesize * ($ page-1). rows is the number of records to be displayed. here is $ page. That is to say, select * from table limit 10, 10 indicates that 20 records starting from 11th records in the table are taken out.
II. important 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); // Retrieve the total number of records $ rs $ myrow = mysql_fetch_array ($ rs ); $ numrows = $ myrow [0]; // calculate the total number of pages $ pages = intval ($ numrows/$ pagesize ); // if (isset ($ _ GET ['Page']) {$ page = intval ($ _ GET ['Page']);} else {$ page = 1; // otherwise, set it to the first page}
III. create a use case Table 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>