<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> PHP paging </title>
</Head>
<Body>
<? Php
$ Conn = mysql_connect ("localhost", "root", "123456") or die ("database connection error". mysql_error ());
Mysql_select_db ("wangyantest", $ conn) or die ("database access error". mysql_error ());
Mysql_query ("set names gb2312 ");
$ Page = $ _ GET ["page"];
If ($ page = "")
{$ Page = 1 ;}
If (is_numeric ($ page ))
{
$ Page_size = 2; // number of data entries per page
$ Query = "select count (*) as total from test order by id desc ";
$ Result = mysql_query ($ query );
$ Message_count = mysql_result ($ result, 0, "total ");
$ Page_count = ceil ($ message_count/$ page_size );
$ Offset = ($ page-1) * $ page_size;
$ SQL = mysql_query ("select * from test order by id desc limit $ offset, $ page_size ");
$ Row = mysql_fetch_object ($ SQL );
If (! $ Row)
{
Echo ("no data! ");
}
?>
<Ul>
<? Php
Do
{
?>
<Li> <? Php echo "<a href = contnet. php? Id = ". $ row-> id."> ". $ row-> titile." | ". $ row-> content." </a> "?> </Li>
<? Php
} While ($ row = mysql_fetch_object ($ SQL ));
?>
</Ul>
<? Php
}
?>
Current page number: <? Php echo $ page;?> /<? Php echo $ page_count;?>
Number of records: <? Php echo $ message_count;?>
<Hr/>
<? Php
If ($ page! = 1)
{
Echo "<a href = page. php? Page = 1> homepage </a> | ";
Echo "<a href = page. php? Page = ". ($ page-1)."> previous page </a> ";
}
If ($ page <$ page_count)
{
Echo "<a href = page. php? Page = ". ($ page + 1)."> next page </a> | ";
Echo "<a href = page. php? Page = ". $ page_count."> last page </a> ";
}
Mysql_free_result ($ SQL );
Mysql_close ($ conn );
?>
</Body>
</Html>