上一次說了PHP+MYSQL實現內容分頁的代碼,但是項目的需求有時候需要用戶進行搜索,然后展示相應的內容,如果內容太多,也要進行分頁。
最后做完的結果如下:

測試代碼如下,總共分為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.'頁 '.$this->first().$this->pre().$this->next().$this->end();
}
}
?>
數據內容如下,可以自己去配置一個作為測試:

- 0、php安裝
- 1、選擇PHP作為首選后端語言的原因
- 2、PHP基本語法
- 3、PHP中變量和常量的區別
- 4、PHP中單引號和雙引號的區別
- 5、PHP檢測數據類型的幾種方式
- 6、PHP數據類型轉換
- 7、return、break、continue的區別
- 8、PHP代碼重用
- 9、字符串移除或添加函數
- 10、PHP中字符串大小寫切換以及翻轉和加密
- 11、PHP字符串截取和截取函數
- 12、PHP字符串替換和比較函數
- 13、PHP字符串url解析和實體轉換
- 14、addslashes在預定義字符串前添加反斜杠
- 15、PHP中的數組基本概念
- 16、數組函數implode、explode、in_array、each、list
- 17、PHP數組函數(count、reset、end、next、current、key)
- 18、ZendStudio軟件破解安裝包免費下載
- 19、【圖文】ZendStudio漢化方法
- 20、數組函數in_array、array_search、array_change_key_case、array_chu
- 21、關閉PHP提示的方法(運行出現Notice : Use of undefined constant 的完美解決方案
- 22、PHP數組函數(array_diff_ukey、array_diff_uassoc、array_intersect
- 23、PHP數組函數(array_fill、array_filter、array_flip、array_key_exis
- 24、PHP數組去重及向前向后刪除元素的函數
- 25、PHP數組函數(array_map、array_walk、array_walk_recursive)
- 26、PHP數組函數(compact、extract)
- 27、PHP數組函數(array_merge、array_merge_recursive)
- 28、PHP數組函數(range、array_count_values、array_product)
- 29、PHP數組函數(array_reduce、array_slice、array_splice、array_sum)
- 30、PHP數組排序函數總結
- 31、PHP中面向對象的基本概念及定義對象的方法
- 32、PHP創建對象與構造函數
- 33、PHP對象的釋放
- 34、PHP面向對象的特性(抽象、封裝、繼承、多態)
- 35、PHP面向對象的public、private、protected之間的區別
- 36、PHP面向對象中的final和const的用法
- 37、PHP面向對象的static關鍵字
- 38、PHP中的單例模式
- 39、$this、self、parent詳解
- 40、PHP中面向對象的抽象類和抽象方法
- 41、PHP面向對象中的接口interface
- 42、PHP面向對象中的魔術方法
- 43、面向對象方法get_object_vars、is_subclass_of、interface_exists
- 44、PHP中的數學函數方法總結
- 45、PHP文件處理disk_total_space、disk_free_space、dirname、file_exis
- 46、PHP目錄操作rename和scandir
- 47、PHP操作目錄opendir、readdir、glob
- 48、PHP打開fopen、讀取fread、寫入文件fwrite
- 49、PHP文件處理file_get_contents、file_put_contents、fgetc、fgets、fg
- 50、PHP截取字符串出現亂碼的解決方法(UTF8和GBK)
- 51、PHP文件內容分頁操作,避免亂碼
- 52、PHP文件操作函數file、set_include_path、copy
- 53、PHP文件操作函數filemtime、filectime、fileatime、touch
- 54、PHP文件操作指針函數feof、ftell、fseek、rewind、fpassthru
- 55、PHP文件操作pathinfo、realpath、flock、tempnam、tmpfile
- 56、設置php.ini配置實現表單文件上傳流程
- 57、PHP將上傳到臨時文件夾的文件移動到服務器指定文件夾內
- 58、PHP+input表單實現多文件上傳
- 59、PHP通過超鏈接實現文件下載
- 60、發送header文件頭信息實現文件下載
- 61、PHP中的日期時間_時區時間戳函數使用
- 62、PHP操作COOKIE緩沖區影響COOKIE傳遞方式
- 63、PHP中SESSION定義、建立、刪除方式
- 64、PHP.INI配置文件中關于session的設置
- 65、HP中Session緩存的概念與用法
- 66、PHP自定義Session處理機制
- 67、通過MYSQL數據庫操作Session會話完整源碼類
- 68、PHP連接數據庫實現分頁代碼
- 69、HTTP緩存實現原理詳解
- 70、PHP連接數據庫實現搜索+分頁功能
- 71、PHP圖像處理之建立畫布填充顏色、打開不同圖像類型處理
- 72、PHP顏色或圖像填充及不同填充方式
- 73、PHP圖像處理之畫線&定義線樣式&寬度&風格&筆刷
- 74、PHP圖像處理:繪制色素及矩形圖
- 75、PHP圖像處理之畫圓、弧線、網站餅狀統計圖繪制
- 76、PHP圖像處理之繪制多邊行及文字繪制
- 77、PHP 圖像處理之獲得文字寬高屬性&圖片水印函數功能
- 78、PHP生成圖片驗證碼demo【OOP面向對象版本】
- 79、PHP圖像處理 圖像處理之處理文字及獲得文字尺寸屬性,imagecopymerge imagecop
- 80、替換用戶輸入的QQ表情
- 81、PHP封裝MYSQL數據庫操作類源碼
- 82、PHP與XML技術結合使用解析