您不能只將一個數組傳遞給like()
函數,它必須是一個字符串。您需要爲字符串中的每個關鍵字應用like
子句。
您需要使用or_like
至創紀錄的匹配包含關鍵字的,但是,因爲如果你只是用like()
每一次,它需要的全部關鍵詞匹配的,由於查詢是像LIKE "%smart" AND LIKE "%intelligent"
等和不是你所需要的。
$array_string = "smart,intelligent,awesome";
$array_like = explode(',', $array_string);
foreach($array_like as $key => $value) {
if($key == 0) {
$this->db->like('topic', $value);
} else {
$this->db->or_like('topic', $value);
}
}
試試這種方式來避免您的問題,其餘的where
語句被忽略。
$array_string = "smart,intelligent,awesome";
$array_like = explode(',', $array_string);
$like_statements = array();
foreach($array_like as $value) {
$like_statements[] = "topic LIKE '%" . $value . "%'";
}
$like_string = "(" . implode(' OR ', $like_statements) . ")";
的$like_string
值將(topic LIKE '%smart%' OR topic LIKE '%intelligent%' OR topic LIKE '%awesome%')
然後,您可以通過以下方式使用$like_string
用ActiveRecord:
$this->db->where($like_string, FALSE);
'$ array_like'已經因爲'爆炸陣列() ',也許可以改變'$ this-> db-> like('topic',array($ array_like));''到'$ this-> db-> like('topic',$ array_like)';可能是一個好的開始。 – dqlopez
@dqlopez我已經嘗試過了,但再次排列對話通知陣列即將到來 –