PHP paging instance code
<! 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 tutorial make page list </title>
<Style type = "text/CSS">
<! --
. Page a: link {
Color: # 0000FF;
Text-decoration: none;
}
. Page a: visited {
Text-decoration: none;
Color: # 0000FF;
}
. Page a: hover {
Text-decoration: none;
Color: # 0000FF;
}
. Page a: active {
Text-decoration: none;
Color: # 0000FF;
}
. Page {color: # 0000FF ;}
-->
</Style>
</Head>
<Body>
<Table width = "530" height = "103" border = "0" align = "center" cellpadding = "0" cellspacing = "1" bgcolor = "# CCCCCC">
<Tr>
<Th width = "30" height = "38" bgcolor = "# E3E3E3" scope = "col"> ID </th>
<Th width = "500" bgcolor = "# E3E3E3" scope = "col"> Article Title </th>
</Tr>
<? Php
/*
* Created on 2010-4-17
*
* Order by Kove Wong
*/
$ Link = MySQL_connect ('localhost', 'root', 'haoxiazai1987 ');
Mysql tutorial _ select_db ('pagelist ');
Mysql_query ('set names gbk ');
$ Page_size = 10;
$ Result = mysql_query ('select * from v_char ');
$ Count = mysql_num_rows ($ result );
$ Page_count = ceil ($ count/$ Page_size );
$ Init = 1;
$ Page_len = 7;
$ Max_p = $ page_count;
$ Pages = $ page_count;
// Determine the current page number
If (empty ($ _ GET ['page']) | $ _ GET ['page'] <0 ){
$ Page = 1;
} Else {
$ Page = $ _ GET ['page'];
}
$ Offset = $ Page_size * ($ page-1 );
$ SQL = "select * from v_char limit $ offset, $ Page_size ";
$ Result = mysql_query ($ SQL, $ link );
While ($ row = mysql_fetch_array ($ result )){
?>
<Tr>
<Td bgcolor = "# E0EEE0" height = "25px"> <div align = "center">
<? Php echo $ row ['id']?>
</Div> </td>
<Td bgcolor = "# E0EEE"> <div align = "center">
<? Php echo $ row ['name']?>
</Div> </td>
</Tr>
<? Php
}
$ Page_len = ($ page_len % 2 )? $ Page_len: $ pagelen + 1; // number of page numbers
$ Pageoffset = ($ page_len-1)/2; // page number offset between left and right
$ Key = '<div class = "page"> ';
$ Key. = "<span> $ page/$ pages </span> & nbsp;"; // page
If ($ page! = 1 ){
$ Key. = "<a href =" ". $ _ SERVER ['php _ SELF ']."? Page = 1 "> page 1 </a>"; // page 1
$ Key. = "<a href =" ". $ _ SERVER ['php _ SELF ']."? Page = ". ($ page-1)." "> previous page </a>"; // Previous page
} Else {
$ Key. = "first page"; // first page
$ Key. = "Previous Page"; // Previous Page
}
If ($ pages> $ page_len ){
// If the current page is less than or equal to the left offset
If ($ page <= $ pageoffset ){
$ Init = 1;
$ Max_p = $ page_len;
} Else {// if the current page is greater than the left offset
// If the right offset of the current page number exceeds the maximum page number
If ($ page + $ pageoffset >=$ pages + 1 ){
$ Init = $ pages-$ page_len + 1;
} Else {
// Calculation when both the left and right offsets exist
$ Init = $ page-$ pageoffset;
$ Max_p = $ page + $ pageoffset;
}
}
}
For ($ I = $ init; $ I <= $ max_p; $ I ++ ){
If ($ I = $ page ){
$ Key. = '<span>'. $ I. '</span> ';
} Else {
$ Key. = "<a href =" ". $ _ SERVER ['php _ SELF ']."? Page = ". $ I." ">". $ I. "</a> ";
}
}
If ($ page! = $ Pages ){
$ Key. = "<a href =" ". $ _ SERVER ['php _ SELF ']."? Page = ". ($ page + 1)." "> next page </a>"; // next page
$ Key. = "<a href =" ". $ _ SERVER ['php _ SELF ']."? Page = {$ pages} "> last page </a>"; // last page
} Else {
$ Key. = "next page"; // next page
$ Key. = "last page"; // last page
}
$ Key. = '</div> ';
?>
<Tr>
<Td colspan = "2" bgcolor = "# E0EEE0"> <div align = "center"> <? Php echo $ key?> </Div> </td>
</Tr>
</Table>
</Body>
</Html>