Paging in php is something we must use in development. However, it is much easier to use pagination in php than in asp, below I will give you a detailed introduction to several good php paging classes. You can refer to them. Paging in php is something we must use in development. However, it is much easier to use pagination in php than in asp, below I will give you a detailed introduction to several good php paging classes. You can refer to them.
Script ec (2); script
This is my own php paging instance.
| The Code is as follows: |
|
|
Simple PHP paging Program
// Create a database connection
$ LinkID = @ mysql_connect ("itlobo.com", "root", "") or die ("you cocould notconnect mysql ");
// Name of the connected database
@ Mysql_select_db ("ceshi") or die ("cocould not select database! ");
// Retrieve the total number of records
$ Query = "SELECT count (*) FROM user ";
$ Rs = mysql_query ($ query );
$ Myrows = mysql_fetch_array ($ rs );
$ Numrows = $ myrows [0];
// Set the number of records displayed on each page
$ Pagesize = 1;
// Calculate the total number of pages
$ Pages = intval ($ numrows/$ pagesize );
If ($ numrows % $ pagesize)
$ Pages ++;
// Set the page number
If (isset ($ _ POST ['page'])
$ Page = intval ($ _ POST ['page']);
Else {
$ Page = 1; // the first page is displayed if no pages exist;
}
// Calculate the record offset
$ Offset = ($ page-1) * $ pagesize;
// Read the specified number of records
$ Query1 = "select * from user limit $ offset, $ pagesize ";
$ Rs = mysql_query ($ query1 );
If ($ myrows = mysql_fetch_array ($ rs ))
{
$ I = 0;
?>
| User Name |
Password |
Do {$ I ++;?>
|
|
}While ($ myrows = mysql_fetch_array ($ rs ));Echo"
";
}
$ First = 1;
$ Prev = $ page-1;
$ Next = $ page + 1;
$ Last = $ pages;
If ($ page> 1)
{
Echo "Homepage ";
Echo "Previous Page ";
}
If ($ page <$ pages)
{
Echo "next page ";
Echo "last page ";
}
Echo"
A total of ". $ pages." pages (". $ page."/". $ pages .")";
For ($ I = 1; $ I <$ page; $ I ++)
Echo "[". $ I. "]";
Echo "[". $ page. "]";
For ($ I = $ page + 1; $ I <= $ pages; $ I ++)
Echo "[". $ I. "]";
Echo" ";
?>
|
The above Code cannot be used repeatedly, and a class is sorted out later.
| The Code is as follows: |
|
|
# Pages {display: block; text-align: center; overflow: hidden; color: #000; font-size: 13px}
# Pages a {color: #333; text-decoration: none; font-family: Verdana, Geneva, sans-serif; padding: 2px 3px; display: block}
# Pages li {float: left; display: inline-block; border: 1px solid #999; margin-right: 3px; margin-left: 3px}
# Pages # spages {background: # CCC; font-weight: bold; color: # 36C} |
PHP paging function class source code:
| The Code is as follows: |
|
|
/*
* Paging Module
*/
Interface Page {
Public function showpage ();
}
Class AdminPage implements Page {
/*
* Construction Parameters
* SQL statement, number of entries displayed per page, url
*/
Private $ SQL;
Private $ pageline; // how many rows of data are displayed on each page
Private $ urlstr;
Private $ nowpage;
Private $ totalNum;
Private $ pageNum;
Private $ pageStr; // form a paging string
Public function _ construct ($ SQL, $ p ){
$ This-> SQL = $ SQL;
$ This-> pageline = $ p;
$ This-> urlstr = $ _ SERVER ['script _ name'];
If (empty ($ _ GET ['P']) {
$ This-> nowpage = 1; // initialize the current page number to 1
} Else if (is_numeric ($ _ GET ['P']) {
$ This-> nowpage = $ _ GET ['P'];
} Else {
$ E = new Error (6 );
$ E-> show ();
}
$ S = new SQL ();
$ This-> totalNum = $ s-> results_exist_num ($ this-> SQL );
$ This-> pageNum = ceil ($ this-> totalNum/$ this-> pageline );
}
/*
* Format:
* Homepage | Previous Page | 1 | 2 | 3 | 4 | 5 | next page | last page
*
*
*/
Public function showpage (){
If ($ this-> pageNum >=1 & $ this-> nowpage <5 ){
If ($ this-> pageNum <= 5 ){
$ This-> pageStr ='
- Urlstr .'? P = 1 "target =" rframe "> Homepage
';
If ($ this-> nowpage! = 1 ){
$ This-> pageStr. ='
- Urlstr .'? P = '. ($ this-> nowpage-1).' "target =" rframe "> previous
';
}
For ($ I = 1; $ I <= $ this-> pageNum; $ I ++ ){
If ($ I ==$ this-> nowpage ){
$ This-> pageStr. ='
- Urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I .'
';
} Else {
$ This-> pageStr. ='
- Urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I .'
';
}
}
If ($ this-> nowpage! = $ This-> pageNum ){
$ This-> pageStr. ='
- Urlstr .'? P = '. ($ this-> nowpage + 1).' "target =" rframe "> next page
';
}
$ This-> pageStr. ='
- Urlstr .'? P = '. $ this-> pageNum.' "target =" rframe "> last page
';
Echo $ this-> pageStr;
} Else if ($ this-> pageNum> 5 ){
$ This-> pageStr ='
- Urlstr .'? P = 1 "target =" rframe "> Homepage
';
If ($ this-> nowpage! = 1 ){
$ This-> pageStr. ='
- Urlstr .'? P = '. ($ this-> nowpage-1).' "target =" rframe "> previous
';
}
For ($ I = 1; $ I <= 5; $ I ++ ){
If ($ I ==$ this-> nowpage ){
$ This-> pageStr. ='
- Urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I .'
';
} Else {
$ This-> pageStr. ='
- Urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I .'
';
}
}
If ($ this-> nowpage! = $ This-> pageNum ){
$ This-> pageStr. ='
- Urlstr .'? P = '. ($ this-> nowpage + 1).' "target =" rframe "> next page
';
}
$ This-> pageStr. ='
- Urlstr .'? P = '. $ this-> pageNum.' "target =" rframe "> last page
';
Echo $ this-> pageStr;
}
} Else if ($ this-> nowpage <= $ this-> pageNum-2 & $ this-> nowpage> = 5 ){
$ This-> pageStr ='
- Urlstr .'? P = 1 "target =" rframe "> Homepage
';
$ This-> pageStr. ='
- Urlstr .'? P = '. ($ this-> nowpage-1).' "target =" rframe "> previous
';
For ($ I = $ this-> nowpage-2; $ I <= $ this-> nowpage + 2; $ I ++ ){
If ($ I ==$ this-> nowpage ){
$ This-> pageStr. ='
- Urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I .'
';
} Else {
$ This-> pageStr. ='
- Urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I .'
';
}
}
$ This-> pageStr. ='
- Urlstr .'? P = '. ($ this-> nowpage + 1).' "target =" rframe "> next page
';
$ This-> pageStr. ='
- Urlstr .'? P = '. $ this-> pageNum.' "target =" rframe "> last page
';
Echo $ this-> pageStr;
} Else if ($ this-> nowpage >=$ this-> pageNum-3 & $ this-> nowpage <= $ this-> pageNum ){
$ This-> pageStr ='
- Urlstr .'? P = 1 "target =" rframe "> Homepage
';
$ This-> pageStr. ='
- Urlstr .'? P = '. ($ this-> nowpage-1).' "target =" rframe "> previous
';
For ($ I = $ this-> pageNum-4; $ I <= $ this-> pageNum; $ I ++ ){
If ($ I ==$ this-> nowpage ){
$ This-> pageStr. ='
- Urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I .'
';
} Else {
$ This-> pageStr. ='
- Urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I .'
';
}
}
If ($ this-> nowpage! = $ This-> pageNum ){
$ This-> pageStr. ='
- Urlstr .'? P = '. ($ this-> nowpage + 1).' "target =" rframe "> next page
';
}
$ This-> pageStr. ='
- Urlstr .'? P = '. $ this-> pageNum.' "target =" rframe "> last page
';
Echo $ this-> pageStr;
}
}
}
|