PHP + MySQL paging display example analysis _ PHP Tutorial

Source: Internet
Author: User
PHP + MySQL page display example analysis. 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) web development is a mainstream feature in distributed program development in the future, in general, web development (which accounts for a large proportion of current program development) involves dealing with databases. the client reads data from the server is usually displayed in pages, one-page reading 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 );

Middleware (which accounts for a large proportion in current program development) is the mainstream of distributed program development in the future, and usually web development (which accounts for a large proportion in current program development) all involved...

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.