2013-07-25 39 views
1

正如標題所說,我可以把'%'在笨如何在codeigniter中設置通配符類似查詢的通配符選項?

$this->db->like('title', 'match', 'before'); 
// Produces: WHERE title LIKE '%match' 

的類似查詢關聯數組

$array = array('title' => $match, 'page1' => $match, 'page2' => $match); 

    $this->db->like($array); 

    // WHERE title LIKE '%match%' AND page1 LIKE '%match%' AND page2 LIKE '%match%' 

更多的澄清我的模型具有的功能在我發送一個陣列,可處理大部分選擇查詢參數來檢索結果

function getTableData($table='', $fields='', $like=array(),$like1=array()) 
{ 

    //Check For like statement 
    if(is_array($like) and count($like)>0)  
     $this->db->like($like); 

    if(is_array($like1) and count($like1)>0) 

     $this->db->or_like($like1); 

    //Check For Fields 
    if($fields!='') 

      $this->db->select($fields); 

    else   
     $this->db->select(); 

    $result = $this->db->get(); 

//pr($result->result()); 
    return $result; 

} 

這是我的通用函數,所以在發送參數或mod ifying函數如何使用通配符放置第三個參數,默認'both'按原樣運行。

與第三參數i控制%載置,但是當我使用一個關聯陣列如何可以實現通配符笨放置。 我如何在關聯數組中使用它,以適用於不同的列。是否有可能?我知道我可以使用自定義查詢,目前我正在使用它。任何幫助提前致謝。

回答

1

使用盡可能多的like,你在你的查詢需要。例如:

$this->db->like('title', $title, 'after')->like('page1', $page1)->like('page2', $page2, 'after')->get('table'); 
+1

我知道我可以像使用很多次是有可能assoc命令陣列我的通用功能 –

2

我看了核心DB_active_rec.php文件。請試試這個:

$this->db->like($array, false, 'before'); 

/system/database/DB_active_rec.php行571:

/** 
* Like 
* 
* Generates a %LIKE% portion of the query. Separates 
* multiple calls with AND 
* 
* @param mixed 
* @param mixed 
* @return object 
*/ 
public function like($field, $match = '', $side = 'both') 
{ 
    return $this->_like($field, $match, 'AND ', $side); 
} 
+0

'CI_DB_active_record ::像()'只接受3個參數。 '_like()'方法是'protected'。 –

+0

起初我寫的是真的。你是對的!更新。 – Bora

0

您可以使用像()幾次:

$this->db->like('title', 'match'); 
$this->db->like('page1', 'match'); 
$this->db->like('page2', 'match'); 

,或者PHP> 5使用鏈接:

$this->db->like('title', 'match')->like('page1', 'match')->like('page2', 'match'); 

如果您有值的陣列,使用的foreach():

//considering $like = array('field' => 'value', 'field2' => 'value2'); 
foreach ($like as $field=> $value) 
{ 
    $this->db->like($field, $value); 
}