Display page number encapsulation in php and page number encapsulation in php
The example in this article shares with you the pagination class of page number encapsulated by php for your reference. The specific content is as follows:
I. Code
Conn. php
<?php class Mysql{ public function __construct(){ $this->connect(); } public function connect(){ $conn=mysql_pconnect('localhost','root','root') or die("Connect MySQL False"); mysql_select_db('db_database20',$conn) or die("Connect DB False"); mysql_query("SET NAMES utf8"); } } ?>
Index. php
<Link rel = "stylesheet" type = "text/css" href = "css/style.css" rel = "external nofollow"> <? Php include_once ("conn. php "); // contains conn. PHP File class Page extends Mysql {// create a Page class and inherit the Mysql class private $ pagesize; // number of records displayed on each page private $ Page; // The current page is private $ pages; // the total number of pages private $ total; // The total number of queried records private $ pagelen; // The number of displayed pages private $ pageoffset; // offset of the page number private $ table; // The name of the table to be queried function _ construct ($ pagesize, $ pagelen, $ table) {if ($ _ GET ['page'] = "" | $ _ GET ['page'] <0) {// determine whether the address bar parameter page has a value $ this-> page = 1; // the current page is defined as 1} els E {$ this-> page =$ _ GET ['page']; // the current page is the address bar parameter value} $ this-> pagesize = $ pagesize; $ this-> pagelen = $ pagelen; $ this-> table = $ table; new Mysql (); // instantiate Mysql class $ SQL = mysql_query ("select * from $ this-> table"); // query the records in the table $ this-> total = mysql_num_rows ($ SQL ); // obtain the total number of queried records $ this-> pages = ceil ($ this-> total/$ this-> pagesize ); // calculate the total number of pages $ this-> pageoffset = ($ this-> pagelen-1)/2; // calculate the page offset} function sel () {$ SQL = mysql_query ("select * from $ This-> table limit ". ($ this-> page-1) * $ this-> pagesize. ",". $ this-> pagesize); // query the records displayed on the current page: return $ SQL; // return the query result} function myPage () {$ message = "". $ this-> page. "Page/total ". $ this-> pages. "page"; // the current page number. The total page number is if ($ this-> page = 1) {// if the current page is 1 $ message. = "homepage previous page"; // output text without link} else {$ message. = "<a href = '". $ _ SERVER ['php _ SELF ']. "? Page = 1'> homepage </a> "; // output the link text $ message. = "<a href = '". $ _ SERVER ['php _ SELF ']. "? Page = ". ($ this-> page-1 ). "'> previous page </a>"; // output link text} if ($ this-> page <= $ this-> pageoffset) {// if the current page is less than the offset of the page number $ minpage = 1; // The minimum displayed page number is 1 $ maxpage = $ this-> pagelen; // The maximum number of displayed pages is the page number value} elseif ($ this-> page> $ this-> pages-$ this-> pageoffset) {// if the current page is greater than the total page number minus the offset of the page number $ minpage = $ this-> pages-$ this-> pagelen + 1; // The minimum number of displayed pages is the total number of pages minus the number of pages plus 1 $ maxpage = $ this-> pages; // The maximum number of displayed pages is the total number of pages} else {$ minpage = $ this-> page-$ this-> pageoffset; // The minimum displayed page number is the offset of the current page number minus the page number $ Maxpage = $ this-> page + $ this-> pageoffset; // The maximum number of displayed pages is the offset of the current page plus the page number} for ($ I = $ minpage; $ I <= $ maxpage; $ I ++) {// Number of numbers displayed in a loop if ($ I ==$ this-> page) {$ message. = $ I. "\ n"; // outputs numbers without links} else {$ message. = "<a id = 'num' href = '". $ _ SERVER ['php _ SELF ']. "? Page = ". $ I. "'> ". $ I. "</a> \ n"; // output a number with a link }}if ($ this-> page ==$ this-> pages) {// if the current page is equal to the maximum page number $ message. = "last page of the next page"; // displays unlinked text} else {$ message. = "<a href = '". $ _ SERVER ['php _ SELF ']. "? Page = ". ($ this-> page + 1 ). "'> next page </a>"; // displays the link text $ message. = "<a href = '". $ _ SERVER ['php _ SELF ']. "? Page = ". $ this-> pages. "'> last page </a>"; // display link text} return $ message; // return variable value }}?> <Table border = "1" cellpadding = "1" cellspacing = "1" bordercolor = "# FFFFFF" bgcolor = "# FF0000"> <tr> <td style = "padding- left: 3px; padding-right: 3px; padding-top: 3px; padding-bottom: 3px; "bgcolor =" # FFFFFF "> ID: </td> <td style = "padding-left: 3px; padding-right: 3px; padding-top: 3px; padding-bottom: 3px; "bgcolor =" # FFFFFF "> title </td> <td style =" padding-left: 3px; padding-right: 3px; padding-top: 3px; padding-bottom: 3px; "bgcolor =" # FFFFFF "> content </td> <td style =" padding-left: 3px; padding-right: 3px; padding-top: 3px; padding-bottom: 3px; "bgcolor =" # FFFFFF "> time </td> </tr> <? Php $ p = new Page ('3', '3', 'tb _ demo01 '); $ rs = $ p-> sel (); while ($ rst = mysql_fetch_row ($ rs) {?> <Tr> <td style = "padding-left: 3px; padding-right: 3px; padding-top: 3px; padding-bottom: 3px; "bgcolor =" # FFFFFF "> <? Php echo $ rst [0]?> </Td> <td style = "padding-left: 3px; padding-right: 3px; padding-top: 3px; padding-bottom: 3px; "bgcolor =" # FFFFFF "> <? Php echo $ rst [1]?> </Td> <td style = "padding-left: 3px; padding-right: 3px; padding-top: 3px; padding-bottom: 3px; "bgcolor =" # FFFFFF "> <? Php echo $ rst [2]?> </Td> <td style = "padding-left: 3px; padding-right: 3px; padding-top: 3px; padding-bottom: 3px; "bgcolor =" # FFFFFF "> <? Php echo $ rst [3]?> </Td> </tr> <? Php }?> </Table> <? Php echo $ p-> myPage ();?>
Ii. Running result
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.