Php paging instance program functions

Source: Internet
Author: User
Tags prev

This is my own php paging instance.

 

The code is as follows: Copy code

<Html>
<Head>
<Title> simple PHP paging program </title>
</Head>
<Body>
<? Php
// 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;
?>
<Table width = "80%" border = "1">
<Tr>
<Td width = "50%"> user name </td>
<Td width = "50%" type = "codeph" text = "codeph"> password </td type = "codeph" text = "/codeph">
</Tr>
<? Php
Do {
$ I ++;
?>
<Tr>
<Td width = "50%"> <? Php echo $ myrows ["username"]?> </Td>
<Td width = "50%"> <? Php echo $ myrows ["password"]?> </Td>
</Tr>
<? Php
        }
While ($ myrows = mysql_fetch_array ($ rs ));
Echo "</table> ";
    } 
$ 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> ";
        }
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>

The above code cannot be used repeatedly, and a class is sorted out later.

The code is as follows: Copy code

# 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: Copy code

<? Php
/*
* 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 = '<ul id = "pages"> <li> <a href = "'. $ this-> urlstr .'? P = 1 "target =" rframe "> homepage </a> </li> ';
If ($ this-> nowpage! = 1 ){
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. ($ this-> nowpage-1).' "target =" rframe "> Previous Page </a> </li> ';
}
For ($ I = 1; $ I <= $ this-> pageNum; $ I ++ ){
If ($ I ==$ this-> nowpage ){
$ This-> pageStr. = '<li id = "spages"> <a href = "'. $ this-> urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I.' </a> </li> ';
} Else {
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I.' </a> </li> ';
}
}
If ($ this-> nowpage! = $ This-> pageNum ){
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. ($ this-> nowpage + 1).' "target =" rframe "> Next page </a> </li> ';
}

$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. $ this-> pageNum.' "target =" rframe "> Last page </a> </li> ';
Echo $ this-> pageStr;

} Else if ($ this-> pageNum> 5 ){

$ This-> pageStr = '<ul id = "pages"> <li> <a href = "'. $ this-> urlstr .'? P = 1 "target =" rframe "> homepage </a> </li> ';
If ($ this-> nowpage! = 1 ){
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. ($ this-> nowpage-1).' "target =" rframe "> Previous Page </a> </li> ';
}

For ($ I = 1; $ I <= 5; $ I ++ ){
If ($ I ==$ this-> nowpage ){
$ This-> pageStr. = '<li id = "spages"> <a href = "'. $ this-> urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I.' </a> </li> ';
} Else {
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I.' </a> </li> ';
}
}
If ($ this-> nowpage! = $ This-> pageNum ){
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. ($ this-> nowpage + 1).' "target =" rframe "> Next page </a> </li> ';
}

$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. $ this-> pageNum.' "target =" rframe "> Last page </a> </li> ';
Echo $ this-> pageStr;
}

} Else if ($ this-> nowpage <= $ this-> pageNum-2 & $ this-> nowpage> = 5 ){
$ This-> pageStr = '<ul id = "pages"> <li> <a href = "'. $ this-> urlstr .'? P = 1 "target =" rframe "> homepage </a> </li> ';
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. ($ this-> nowpage-1).' "target =" rframe "> Previous Page </a> </li> ';
For ($ I = $ this-> nowpage-2; $ I <= $ this-> nowpage + 2; $ I ++ ){
If ($ I ==$ this-> nowpage ){
$ This-> pageStr. = '<li id = "spages"> <a href = "'. $ this-> urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I.' </a> </li> ';
} Else {
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I.' </a> </li> ';
}
}
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. ($ this-> nowpage + 1).' "target =" rframe "> Next page </a> </li> ';
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. $ this-> pageNum.' "target =" rframe "> Last page </a> </li> ';
Echo $ this-> pageStr;

} Else if ($ this-> nowpage >=$ this-> pageNum-3 & $ this-> nowpage <= $ this-> pageNum ){

$ This-> pageStr = '<ul id = "pages"> <li> <a href = "'. $ this-> urlstr .'? P = 1 "target =" rframe "> homepage </a> </li> ';
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. ($ this-> nowpage-1).' "target =" rframe "> Previous Page </a> </li> ';

For ($ I = $ this-> pageNum-4; $ I <= $ this-> pageNum; $ I ++ ){
If ($ I ==$ this-> nowpage ){
$ This-> pageStr. = '<li id = "spages"> <a href = "'. $ this-> urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I.' </a> </li> ';
} Else {
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. $ I.' "target =" rframe "> '. $ I.' </a> </li> ';
}
}
If ($ this-> nowpage! = $ This-> pageNum ){
$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. ($ this-> nowpage + 1).' "target =" rframe "> Next page </a> </li> ';
}

$ This-> pageStr. = '<li> <a href = "'. $ this-> urlstr .'? P = '. $ this-> pageNum.' "target =" rframe "> Last page </a> </li> ';
Echo $ this-> pageStr;

}
}
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.