Analysis of PHPMySQL paging display examples in web development-PHP source code

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

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.