PHP + MySQL paging display example analysis

Source: Internet
Author: User
PHP + MySQL paging display example analysis, read the PHP + MySQL paging display example analysis, Web development is the mainstream of distributed program development in the future, usually web development involves dealing with databases, client reads from the server are usually displayed in pages. reading one page is both convenient and beautiful. Therefore, the paging program is developed by the web. <LINKhref = "http: // www.

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

$ 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_query ("select * from myTable order by id desc limit $ offset, $ pagesize", $ conn );
If ($ myrow = mysql_fetch_array ($ rs ))
{
$ I = 0;
?>
<Table border = "0" width = "80%">
<Tr>
& Lt; td width = "50%" bgcolor = "# E0E0E0" & gt;
<P align = "center"> title </td>
& Lt; td width = "50%" bgcolor = "# E0E0E0" & gt;
<P align = "center"> release date </td>
</Tr>
<? Php
Do {
$ I ++;
?>
<Tr>
<Td width = "50%"> <? = $ Myrow ["news_title"]?> </Td>
<Td width = "50%"> <? = $ Myrow ["news_cont"]?> </Td>
</Tr>
<? Php
}
While ($ myrow = mysql_fetch_array ($ rs ));
Echo "</table> ";
}
Echo "<div align = 'center'> Total". $ pages. "page (". $ page. "/". $ pages .")";
For ($ I = 1; $ I <$ page; $ I ++)
Echo "<a href = 'fenye. php? Page = ". $ I." '> [". $ I."] </a> ";
Echo "[". $ page. "]";
For ($ I = $ page + 1; $ I <= $ pages; $ I ++)
Echo "<a href = 'fenye. php? Page = ". $ I." '> [". $ I."] </a> ";
Echo "</div> ";
?>
</Body>
</Html>

V. Summary

The code in this example runs normally on windows2000 server + php4.4.0 + mysql5.0.16. The page format displayed in this example is [1] [2] [3]… This form. If you want to display the page as "the last page of the previous page on the homepage", add the following code:

$ First = 1;
$ Prev = $ page-1;
$ Next = $ page + 1;
$ Last = $ pages;

If ($ page> 1)
{
Echo "<a href = 'fenye. php? Page = ". $ first." '> homepage </a> ";
Echo "<a href = 'fenye. php? Page = ". $ prev." '> Previous page </a> ";
}

If ($ page <$ pages)
{
Echo "<a href = 'fenye. php? Page = ". $ next." '> next page </a>
Echo "<a href = 'fenye. php? Page = ". $ last." '> last page </a> ";
}

In fact, it is very easy to write the paging display code, as long as you master its working principle. I hope this article will help web programmers who need it.

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.