<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                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)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看