標籤:
PHP搜尋加分頁瀏覽小結:1 分頁後再做搜尋2 這裡對於url的拼接,以及模糊查詢,搜尋時候的顯示添加,SQL語句的拼接3 對於頁面傳遞過來的超級連結的變數,如果不存在就要設定,對於可能拋出異常的要加上@屏蔽錯誤4 對於查詢一般用一些關鍵字來擷取5 分頁顯示的limit條件要寫好6 搜尋時候的where條件搜尋表單:<!--- 搜尋表單---><form action="list3.php" method="get">標題:<input type="text" name="title" size="10" value="<?php echo @$_GET[‘title‘];?>"/> 關鍵字:<input type="text" name="keywords" size="10" value="<?php echo @$_GET[‘keywords‘];?>"/> <input type="text" name="author" size="10" value="<?php echo @$_GET[‘author‘];?>"/> <input type="submit" value="搜尋"/><input type="button" value="全部資訊" onclick="window.location=‘list3.php‘"/></form><!---------------->分頁封裝<?php//=======================//封裝搜尋的資訊//定義一個封裝搜尋條件的陣列變數$wherelist=array();//定義一個封裝搜尋的url,用於放置到url後面作為參數$urllist=array();//判斷新聞標題是否有值就封裝搜尋條件if(!empty($_GET[‘title‘])){$wherelist[]="title like ‘%{$_GET[‘title‘]}%‘";$urllist[]="title={$_GET[‘title‘]}";}//判斷關鍵字是否有值就封裝搜尋條件if(!empty($_GET[‘keywords‘])){$wherelist[]="keywords like ‘%{$_GET[‘keywords‘]}%‘";$urllist[]="keywords={$_GET[‘keywords‘]}";}//判斷作者是否有值就封裝搜尋條件if(!empty($_GET[‘author‘])){$wherelist[]="author like ‘%{$_GET[‘author‘]}%‘";$urllist[]="author={$_GET[‘author‘]}";}//組裝搜尋條件//將數組合并成字串用implode();if(count($wherelist)>0){$where=" where ".implode(" and ",$wherelist);[email protected]"&".implode("&",$urllist);}//echo @$where;//echo @$url;//=======================?>3 分頁處理<?php//=====插入分頁處理代碼=====//1 定義分頁的變數$page=isset($_GET[‘page‘])?$_GET[‘page‘]:1;//當前頁數,預設為1$pageSize=4;//頁大小$maxRows="";//最大資料條數$maxPages="";//最大頁數//擷取最大資料條數@$sql="select count(*) from news {$where}";$res=mysql_query($sql,$conn);$maxRows=mysql_result($res,0,0);//定位從結果集中擷取總資料的條數,就是擷取第一個儲存格中的值//3 計算出最大頁數$maxPages=ceil($maxRows/$pageSize);//進一取整擷取最大頁數,7/3;//4 判斷頁數是否越界,判斷是否有效if($page>$maxPages){$page=$maxPages;//判斷是否超出了最大頁}if($page<1){$page=1;}//拼接$sql,限制每頁顯示的條數$limit=" limit ".(($page-1)*$pageSize).",{$pageSize}";//起始位置是當前頁減1乘以每頁顯示的條數//==========分頁封裝結束==============@$sql="select * from news {$where} order by addtime desc {$limit}";//limit 0,3表示從第一條記錄到第三條記錄//將最新的新聞先顯示出來[email protected]_query($sql,$conn);?>具體執行個體:新聞搜尋和分頁程式:搜尋和分頁功能如下:list3.php 完整代碼<?phpheader("content-Type:text/html;charset=utf-8");?><?phprequire("menu.php");require("dbconfig.php");?><title>新聞資訊管理系統</title><script>function dodel(id){//判斷是否要刪除if(confirm("確定要刪除嗎?")){window.location="action.php?action=del&id="+id;}}</script><center><h2>搜尋和分頁瀏覽新聞</h2><!--- 搜尋表單---><form action="list3.php" method="get">標題:<input type="text" name="title" size="10" value="<?php echo @$_GET[‘title‘];?>"/> 關鍵字:<input type="text" name="keywords" size="10" value="<?php echo @$_GET[‘keywords‘];?>"/> <input type="text" name="author" size="10" value="<?php echo @$_GET[‘author‘];?>"/> <input type="submit" value="搜尋"/><input type="button" value="全部資訊" onclick="window.location=‘list3.php‘"/></form><!----------------><table border="1" cellpadding="2" cellspacing="0"><tr><th>新聞id號</th><th>新聞標題</th><th>發行者</th><th>關鍵字</th><th>添加時間</th><th>新聞內容</th><th>操作</th></tr><?php//=======================//封裝搜尋的資訊//定義一個封裝搜尋條件的陣列變數$wherelist=array();//定義一個封裝搜尋的url,用於放置到url後面作為參數$urllist=array();//判斷新聞標題是否有值就封裝搜尋條件if(!empty($_GET[‘title‘])){$wherelist[]="title like ‘%{$_GET[‘title‘]}%‘";$urllist[]="title={$_GET[‘title‘]}";}//判斷關鍵字是否有值就封裝搜尋條件if(!empty($_GET[‘keywords‘])){$wherelist[]="keywords like ‘%{$_GET[‘keywords‘]}%‘";$urllist[]="keywords={$_GET[‘keywords‘]}";}//判斷作者是否有值就封裝搜尋條件if(!empty($_GET[‘author‘])){$wherelist[]="author like ‘%{$_GET[‘author‘]}%‘";$urllist[]="author={$_GET[‘author‘]}";}//組裝搜尋條件//將數組合并成字串用implode();if(count($wherelist)>0){$where=" where ".implode(" and ",$wherelist);[email protected]"&".implode("&",$urllist);}//echo @$where;//echo @$url;//=======================?><?php//=====插入分頁處理代碼=====//1 定義分頁的變數$page=isset($_GET[‘page‘])?$_GET[‘page‘]:1;//當前頁數,預設為1$pageSize=4;//頁大小$maxRows="";//最大資料條數$maxPages="";//最大頁數//擷取最大資料條數@$sql="select count(*) from news {$where}";$res=mysql_query($sql,$conn);$maxRows=mysql_result($res,0,0);//定位從結果集中擷取總資料的條數,就是擷取第一個儲存格中的值//3 計算出最大頁數$maxPages=ceil($maxRows/$pageSize);//進一取整擷取最大頁數,7/3;//4 判斷頁數是否越界,判斷是否有效if($page>$maxPages){$page=$maxPages;//判斷是否超出了最大頁}if($page<1){$page=1;}//拼接$sql,限制每頁顯示的條數$limit=" limit ".(($page-1)*$pageSize).",{$pageSize}";//起始位置是當前頁減1乘以每頁顯示的條數//==========分頁封裝結束==============@$sql="select * from news {$where} order by addtime desc {$limit}";//limit 0,3表示從第一條記錄到第三條記錄//將最新的新聞先顯示出來[email protected]_query($sql,$conn);while([email protected]_fetch_assoc($result)){echo "<tr>";echo "<td>{$row[‘id‘]}</td>";echo "<td>{$row[‘title‘]}</td>";echo "<td>{$row[‘author‘]}</td>";echo "<td>{$row[‘keywords‘]}</td>";echo "<td>".date("Y-m-d H:i:s",$row[‘addtime‘])."</td>";echo "<td>{$row[‘content‘]}</td>";echo "<td><a href=‘javascript:dodel({$row[‘id‘]})‘>刪除</a> | <a href=‘edit.php?id={$row[‘id‘]}‘>修改</a></td>";echo "</tr>";}//釋放結果集@mysql_free_result($result);mysql_close($conn);?></table><?php//顯示當前頁數值,上一頁和下一頁echo "<br/>";echo "當前頁 {$page}/{$maxPages}頁 共計:{$maxRows}條 ";?><a href="list3.php?page=1<?php echo @$url;?>">首頁</a> <a href="list3.php?page=<?php echo ($page-1)[email protected]$url;?>">上一頁</a> <!--這裡用分割符號分離出來添加頁數---><a href="list3.php?page=<?php echo ($page+1)[email protected]$url;?>">下一頁</a> <a href="list3.php?page=<?php echo [email protected]$url;?>">尾頁</a></center>
PHP搜尋MYSQL資料庫加分頁瀏覽小結