2014-01-31 24 views
0

我想傳遞數組到活動記錄的like子句中codeignter爲我寫以下:傳遞數組喜歡笨活動記錄

$array_string = smart,intelligent,awesome;

$array_like = explode(',',$array_string);

和在模型:

$this->db->like('topic',array($array_like));

,但我剛開摹

Severity: Notice Message: Array to string conversion

任何幫助或建議將是一個很大的幫助..在此先感謝

看到更新的問題在這裏: join not giving required result when using or_like codeigniter

+0

'$ array_like'已經因爲'爆炸陣列() ',也許可以改變'$ this-> db-> like('topic',array($ array_like));''到'$ this-> db-> like('topic',$ array_like)';可能是一個好的開始。 – dqlopez

+0

@dqlopez我已經嘗試過了,但再次排列對話通知陣列即將到來 –

回答

5

您不能只將一個數組傳遞給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); 
+0

使用'or_like'時,忽略哪裏條件.... –

+0

請參閱我的編輯.... – Ryan

+0

非常感謝你先生我得到databse錯誤::: –

0

假設你正在使用笨2.1.4,正如你可以在CodeIgniter active record documentation中看到的那樣,你不能像第二個參數那樣在函數中傳入數組。你需要傳遞一個字符串作爲參數。

$array_string = "smart,intelligent,awesome"; 
$array_like = explode(',', $array_string); 

foreach ($array_like as $like) 
{ 
    $this->db->like('topic', $like); 
} 
+0

我已經試過這個..這對我有用,當我只通過一個值前:'聰明',當我通過其中3或2前: '聰明,真棒等'然後沒有結果來.. –

+0

這是因爲你可能沒有什麼數據庫匹配'主題像「%智能%」和主題像「智能%%」和主題像「%真棒% 「' – machineaddict

+0

先生,我有數據庫中的值.... –