ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
上一次说了PHP+MYSQL实现内容分页的代码,但是项目的需求有时候需要用户进行搜索,然后展示相应的内容,如果内容太多,也要进行分页。 最后做完的结果如下: ![](https://box.kancloud.cn/86a780c6020afd88d8bee97a9277a9ca_321x173.jpg) 测试代码如下,总共分为2个PHP文件。 第一个:searchPage.php <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> <meta name="keywords" content="" /> <mtea name="description" content="" /> <style type="text/css"> table{margin:10px 0px 0px 0px;} td{border:1px solid #ccc;padding:0px 10px;} </style> </head> <body> <div class="search"> <form method="get" action=""> <input type="text" name="teacherMessage" id="searchTxt" placeholder="老师年龄(20-60)" /> <input type="submit" name="sub" id="searchBtn" value="搜索" /> </form> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <td>ID</td> <td>名字</td> <td>年龄</td> <td>性别</td> </tr> </thead> <tbody> <?php include 'page.php'; header('content-type:text/html;charset="utf-8"'); function show($teacherMessage){ global $teacher; $teacher = new Teacher($teacherMessage); $card = $teacher->getContent(); $len = count($card); if($len==0){ echo '<script type="text/javascript">alert("没有找到您想要的信息")</script>'; }else{ foreach($card as $v){ echo '<tr><td>'.$v['id'].'</td><td>'.$v['tname'].'</td><td>'.$v['tage'].'</td><td>'.$v['tsex'].'</td></tr>'; } } } if($_GET['sub']=='搜索'){ $teacherMessage = $_GET['teacherMessage']; if(!empty($teacherMessage)){ show($teacherMessage); }else{ echo '<script type="text/javascript">alert("内容为空");</script>'; } }else{ show('23'); } ?> </tbody> </table> <div class="pageList"> <?php if($teacher){ echo $teacher->pageStyle(); unset($teacher); } ?> </div> </body> </html> <script type="text/javascript"> var oSearchTxt = document.getElementById('searchTxt'); oSearchTxt.onblur = blurValidate; //鼠标移出输入框,判断输入内容是否为空 function blurValidate(){ if(!this.value){ alert('内容不能为空'); return; } } </script> 第二个:page.php <?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;//数据库信息 private $teacherMessage;//用户输入的搜索信息 public function __construct($teacherMessage){ $this->teacherMessage = $teacherMessage; $this->pdo = $this->getDb(); $query = 'select id,tname,tage,tsex from teacher where tage like "%'.$this->teacherMessage.'%"'; $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; $a = 'zhangsan'; $query = 'select id,tname,tage,tsex from teacher where tage like "%'.$this->teacherMessage.'%" order by id desc 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(); } } ?> 数据内容如下,可以自己去配置一个作为测试: ![](https://box.kancloud.cn/92c017bfa27335168a6c2f8c1f173a39_374x282.jpg =500x500)