Detailed explanation of package graphics and text of PHP display page number and paging class

墨辰丷
Release: 2023-03-27 10:10:01
Original
1370 people have browsed it

This article mainly introduces in detail the PHP encapsulation of a paging class that displays page numbers. It has a certain reference value. Interested friends can refer to it

The specific content is as follows

1. Code

conn.php

<?php 
 class Mysql{ 
  public function __construct(){ 
   $this->connect(); 
  } 
  public function connect(){ 
   $conn=mysql_pconnect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;) or die("Connect MySQL False"); 
   mysql_select_db(&#39;db_database20&#39;,$conn) or die("Connect DB False"); 
   mysql_query("SET NAMES utf8"); 
  } 
 } 
?>
Copy after login

index.php

<link rel="stylesheet" type="text/css" href="css/style.css" rel="external nofollow" > 
<?php 
 include_once("conn.php");//包含conn.php文件 
 class Page extends Mysql{//创建Page类并继承Mysql类 
  private $pagesize;//每页显示的记录数 
  private $page;//当前是第几页 
  private $pages;//总页数 
  private $total;//查询的总记录数 
  private $pagelen;//显示的页码数 
  private $pageoffset;//页码的偏移量 
  private $table;//欲查询的表名 
  function __construct($pagesize,$pagelen,$table){ 
  if($_GET[&#39;page&#39;]=="" || $_GET[&#39;page&#39;]<0){//判断地址栏参数page是否有值 
   $this->page=1;//当前页定义为1 
  }else{ 
   $this->page=$_GET[&#39;page&#39;];//当前页为地址栏参数的值 
  } 
  $this->pagesize=$pagesize; 
  $this->pagelen=$pagelen; 
  $this->table=$table; 
  new Mysql();//实例化Mysql类 
  $sql=mysql_query("select * from $this->table");//查询表中的记录 
  $this->total=mysql_num_rows($sql);//获得查询的总记录数 
  $this->pages=ceil($this->total/$this->pagesize);//计算总页数 
  $this->pageoffset=($this->pagelen-1)/2;//计算页码偏移量 
  } 
  function sel(){ 
  $sql=mysql_query("select * from $this->table limit ".($this->page-1)*$this->pagesize.",".$this->pagesize);//查询当前页显示的记录 
  return $sql;//返回查询结果 
  } 
  function myPage(){ 
  $message="第".$this->page."页/共".$this->pages."页   ";//输出当前第几页,共几页 
  if($this->page==1){//如果当前页是1 
   $message.="首页 上一页   ";//输出没有链接的文字 
  }else{ 
   $message.="<a href=&#39;".$_SERVER[&#39;PHP_SELF&#39;]."?page=1&#39;>首页</a> ";//输出有链接的文字 
   $message.="<a href=&#39;".$_SERVER[&#39;PHP_SELF&#39;]."?page=".($this->page-1)."&#39;>上一页</a>  ";//输出有链接的文字 
  } 
  if($this->page<=$this->pageoffset){//如果当前页小于页码的偏移量 
   $minpage=1;//显示的最小页数为1 
   $maxpage=$this->pagelen;//显示的最大页数为页码的值 
  }elseif($this->page>$this->pages-$this->pageoffset){//如果当前页大于总页数减去页码的偏移量 
   $minpage=$this->pages-$this->pagelen+1;//显示的最小页数为总页数减去页码数再加上1 
   $maxpage=$this->pages;//显示的最大页数为总页数 
  }else{ 
   $minpage=$this->page-$this->pageoffset;//显示的最小页数为当前页数减去页码的偏移量 
   $maxpage=$this->page+$this->pageoffset;//显示的最大页数为当前页数加上页码的偏移量 
  } 
  for($i=$minpage;$i<=$maxpage;$i++){//循环输出数字页码数 
   if($i==$this->page){ 
   $message.=$i."\n";//输出没有链接的数字 
   }else{ 
   $message.="<a id=&#39;num&#39; href=&#39;".$_SERVER[&#39;PHP_SELF&#39;]."?page=".$i."&#39;>".$i."</a>\n";//输出有链接的数字 
   } 
  } 
  if($this->page==$this->pages){//如果当前页等于最大页数 
   $message.="  下一页 尾页";//显示没有链接的文字 
  }else{ 
   $message.="  <a href=&#39;".$_SERVER[&#39;PHP_SELF&#39;]."?page=".($this->page+1)."&#39;>下一页</a> ";//显示有链接的文字 
   $message.="<a href=&#39;".$_SERVER[&#39;PHP_SELF&#39;]."?page=".$this->pages."&#39;>尾页</a>";//显示有链接的文字 
  } 
  return $message;//返回变量的值 
  } 
 } 
?> 
<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">标题</td> 
 <td style="padding-left:3px; padding-right:3px; padding-top:3px; padding-bottom:3px;" bgcolor="#FFFFFF">内容</td> 
 <td style="padding-left:3px; padding-right:3px; padding-top:3px; padding-bottom:3px;" bgcolor="#FFFFFF">时间</td> 
 </tr> 
<?php 
 $p=new Page(&#39;3&#39;,&#39;3&#39;,&#39;tb_demo01&#39;); 
 $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(); 
?>
Copy after login

2. Running results

Related recommendations:

PHPDetailed explanation of the definition and usage of paging class

Detailed explanation of examples phpPaging class

TP5-each function in paging class

The above is the detailed content of Detailed explanation of package graphics and text of PHP display page number and paging class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!