使用对象进行分页显示

xiaoxiao2021-02-28  130

<?php  header("content-type:text/html;charset=utf-8"); class localhost {   private $localhost;   private $prot;   private $user;   private $password;   private $charset;   private $databasename;   private $sql; public function __construct($localhost,$user,$password,$charset,$databasename,$sql,$prot="") {   $this->localhost=$localhost;   $this->prot=$prot;   $this->user=$user;   $this->password=$password;   $this->charset=$charset;   $this->databasename=$databasename;   $this->sql=$sql; }     function getMysqlbefore()     {   if($this->prot=="")   {     $result=@mysql_connect($this->localhost,$this->user,$this->password);     if($result==false)     return "链接数据库失败,请检查主机名,端口,用户名,密码是否正确";     if(!mysql_query("set names $this->charset ")) return "设置编码出现问题,请检查编码";  if(mysql_query("use $this->databasename"))  {             return true;  }else{             return "选择数据库出现问题,请检查库名";  } } else {     $result=@mysql_connect($this->localhost,$this->prot,$this->user,$this->password);     if($result==false)     return "链接数据库失败,请检查主机名,端口,用户名,密码是否正确";     if(!mysql_query("set names $this->charset ")) return "设置编码出现问题,请检查编码";  if(mysql_query("use $this->databasename"))  {             return true;  }else{             return "选择数据库出现问题,请检查库名";      }    }     }     function count()     {             if(mysql_query($this->sql)===false)  return false;         return  mysql_affected_rows();     }     function allover($sql1)     {           $result=mysql_query($sql1);           return $result;     } } class page  {    private $pageall;                                  //数据总条数    private $pagenum;                                  //当前的页码数    private $rowspage;                                 //每页的数据条数    private $pages;                                    //数据总页数    function __construct($pageall,$rowspage,$pagenum)    {     $this->pageall=$pageall;        $this->rowspage=$rowspage;         $this->pagenum=$pagenum;    }    function page_get_str()    {      $this->pages=ceil($this->pageall/$this->rowspage);      $prive=$this->pagenum-1<1 ? 1 : $this->pagenum-1;      $next=$this->pagenum+1>$this->pages ? $this->pages : $this->pagenum+1;      $str="<div>";       $str.="<a style='width: 50px;' href='?id=$prive'>上一页</a>";      for($i=1;$i<=$this->pages;$i++)      {       if($i==$this->pagenum)       {             $str.="<a href='?id=$i' style='color:red'>$i</a>";         }else{         $str.="<a href='?id=$i'>$i</a>";         }      }      $str.="<a style='width: 50px;' href='?id=$next'>下一页</a>";       $str.="</div>";       echo $str;    }        } $i=new localhost(主机名,用户名,密码,编码,库名,查询语句,端口可选); //参数设置(主机名,用户名,密码,编码,库名,查询语句,端口可选) $boo=$i->getMysqlbefore(); if($boo!==true) die($boo); if($count=$i->count())  { $k=$count; }else{ echo "查询语句出错或没有查询到数据"; $k=1; } $id=isset($_GET["id"])? $_GET["id"] : 1; //参数设置(数据总条数,每页的数据条数,当前页码数) $j=new page($k,10,$id); $j->page_get_str(); $result=$i->allover("select * from 表名  order by pub_time desc limit $id,10" ); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>分页</title> </head> <body> <table border="1" > <tr> <th>编号</th> <th>标题</th> <th>内容</th> <th>昵称</th> <th>日期</th> </tr> <?php while($row=mysql_fetch_row($result)){?> <tr> <td><?php echo $row[0]?></td> <td><?php echo $row[1]?></td> <td><?php echo $row[2]?></td> <td><?php echo $row[3]?></td> <td><?php echo date("Y-m-d H:i:s",$row[4])?></td> </tr> <?php } ?> </table> </body> </html>
转载请注明原文地址: https://www.6miu.com/read-59568.html

最新回复(0)