PHP實現搜尋查詢功能的方法技巧
- php語言
- 關注:9.14K次
像這樣簡單的語句select*from table where name like '%$_POST["content"]%';雖然可以實現搜尋功能但是卻不能夠真正應到專案中,為什麼呢?下面,小編為大家搜尋整理了PHP實現搜尋查詢功能的方法技巧,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!
下面是首頁顯示資料表package中的內容,但是有個條件,顯示在首頁的內容還必須是 :欄位status=0,且printing=0的資料才能在首頁列表中顯示出來。
頁面上有一個“搜尋”功能,輸入條件後就會根據條件來進行查詢。
一般的搜尋的話,只要在首頁顯示列表方法index()中給一個:
?
$map=array();//初始化查詢條件
$map=$this->_search();//呼叫查詢方法
$total = $this->Model->where ($map)->count(); //這個主要是用來計算頁面顯示資料條數的
if ($total == 0) {
$_list = '';
} else {
$_list = $this->Model->where ($map)->limit( $post_data ['first'] . ',' . $post_data ['rows'] )->select();
}
然後,就是寫一個_search():
protected function _search(){
$map = array ();
$post_data = I ( 'post.' );
if ($post_data ['packageid'] != '') {
$map ['packageid'] = array (
'like',
'%' . $post_data ['packageid'] . '%'
);
}
return $map;
}
最後,在設定的“搜尋”選單中,呼叫這個搜尋方法。
但是,這個搜尋的'同時,還要確保在欄位status=0,且printing=0的資料中進行搜尋。
這個限制條件該加在什麼地方。各種嘗試和查詢後,才知道。限制條件直接加在SQL語句中就行了(如下紅色的地方)。(我自己試的時候一直在如下藍色的地方加條件,屢試屢敗!)
$map=array();
$map=$this->_search();
$total = $this->Model->where ($map)->where(array('status' =>0,'print_status'=>0))->count();
if ($total == 0) {
$_list = '';
} else {
$_list = $this->Model->where ($map)->where(array('status' =>0,'print_status'=>0))->limit( $post_data ['first'] . ',' . $post_data ['rows'] )->select();
}
- 文章版權屬於文章作者所有,轉載請註明 https://xuezhezhai.com/zh-tw/jsj/php/0lmo23.html