2011-09-15 60 views
0

CodeIgniter應用程序有一個搜索引擎,我想向結果添加條件。 某些代碼是在型號:如何在基於CodeIgniter的搜索引擎上添加條件

$this->db->select(users.country_symbol); 

function getUsers($conditions=array()) 
{ 
    //Check For Conditions 
    if (is_array($conditions) and count($conditions)>0)  
     $this->db->where($conditions); 
} 

控制器上的我:

$users = $this->search_model->getUsers(NULL) 

我怎樣才能改變NULL到,對於請求的條件與$this->loggedInUser->country_symbol具有相同country_symbol的用戶?

回答

0

你可以修改你的函數是這樣的:

function getUsers($conditions = array()) 
{ 
    if (is_array($conditions) && count($conditions) > 0) { 
     foreach ($conditions as $condition) { 
      $this->db->where($condition['where'], $condition['value']); 
     } 
    } 

    // return results... 
} 

然後,你可以這樣做:

$conditions = array(
    array('where' => 'country_symbol =', 'value' => $this->loggedInUser->country_symbol), 
    array('where' => 'zip_code', 'value' => '12345'), 
    // and so on... 
) 

$users = $this->search_model->getUsers($conditions); 

我沒有測試代碼,但是這是基本的想法。您需要遍歷條件數組,並使用$this->db->where()或類似函數應用每個條件。