Ii. MainCodeAnalysis
$ 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.