Complete analysis of the pagination display example of PHPMySQL

Source: Internet
Author: User
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> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.