🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
如果某个类目下有很多条记录,我们不可能一下子全部展现给用户,第一:用户看不完,第二:一次性全部加载性能太差。所以就需要用到翻页技术,用户看哪儿加载哪儿,这个和javascrip结合ajaxt实现瀑布流懒加载有点类似。 下面是我自己写的php分页代码,php代码和html代码放在了一个页面,仅供测试使用,如果你想用在项目里边,可以把对应的类单独放在某个php文件中,然后前端页面include引用即可。 最后做完的截图如下: ![](https://box.kancloud.cn/28a1540db9954e7a1da7ef5fa5b0dacd_363x204.jpg =500x400) 代码如下: <?php header('content-type:text/html;charset="utf-8"'); class Teacher{ private $current;//当前页 private $totalRecord;//总共查询到的记录数 private $pageLen = 3;//每页显示的记录数 private $totalPage;//总页数 private $url;//网址 private $startRecord;//从第几条记录开始查询 private $pdo;//数据库信息 public function __construct(){ $this->pdo = $this->getDb(); $query = 'select * from teacher'; $result = $this->pdo->query($query); $res = $result->fetchAll(PDO::FETCH_ASSOC); $this->current = $_GET['page'] ? $_GET['page'] : 1; $this->url = $this->getUrl(); $this->totalRecord = count($res); $this->totalPage = ceil($this->totalRecord/$this->pageLen); } //连接数据库 private function getDb(){ $pdo = new PDO('mysql:host=localhost;dbname=edu','edu','edu'); return $pdo; } //提取保留url地址中除page之外的其他参数 private function getUrl(){ $url = parse_url($_SERVER['REQUEST_URI']); $query = $url['query']; parse_str($query,$arry); unset($arry['page']); $newQuery = http_build_query($arry); return $url['path'].'?'.$newQuery.'&page='; } //首页 private function first(){ if($this->current>1){ return '<a href="'.$this->url.'1'.'">首页</a>'; } } //上一页 private function pre(){ if($this->current<=$this->totalPage){ return '<a href="'.$this->url.($this->current-1).'">上一页</a>'; } } //下一页 private function next(){ if($this->current<$this->totalPage){ return '<a href="'.$this->url.($this->current+1).'">下一页</a>'; } } //末页 private function end(){ if($this->current<$this->totalPage){ return '<a href="'.$this->url.($this->totalPage).'">末页</a>'; } } //获取表格内容 public function getContent(){ $this->startRecord = 2*$this->current-2; $query = 'select * from teacher where id limit '.$this->startRecord.','.$this->pageLen; $pageResult = $this->pdo->query($query); $pageRes = $pageResult->fetchAll(PDO::FETCH_ASSOC); unset($this->pdo); return $pageRes; } //分页样式 public function pageStyle(){ return '共有'.$this->totalPage.'页&nbsp;&nbsp;&nbsp;'.$this->first().$this->pre().$this->next().$this->end(); } } $teacher = new Teacher(); $card = $teacher->getContent(); ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> <meta name="keywords" content="" /> <meta name="description" content="" /> <style type="text/css"> *{margin:0px;padding:0px;} body{font-size:12px;} .teacherInfo{margin:100px auto 100px 100px;} .teacherInfo .pageList{margin-top:10px;} a{text-decoration:none;margin-right:10px;} a:hover{color:#f00;} td{padding:0px 10px;height:30px;line-height:30px;border:1px solid #ccc;} </style> </head> <body> <div class="teacherInfo"> <table cellpadding="0" cellspacing="0"> <thead> <tr> <td>ID</td> <td>姓名</td> <td>年龄</td> <td>性别</td> </tr> </thead> <tbody> <?php $len = count($card); for($i=0;$i<$len;$i++){ echo '<tr><td>'.$card[$i]["id"].'</td><td>'.$card[$i]["tname"].'</td><td>'.$card[$i]["tage"].'</td><td>'.$card[$i]["tsex"].'</td></tr>'; } ?> </tbody> </table> <div class="pageList"> <?php echo $teacher->pageStyle(); ?> </div> </div> </body> </html> 数据库中的记录截图如下: ![](https://box.kancloud.cn/92c017bfa27335168a6c2f8c1f173a39_374x282.jpg =500x600)