2016-01-29 52 views
0

我有這樣的查詢搜索多個數據,但我希望它是動態的,並連接到我的應用程序離子它只是靜態我將如何將我的搜索查詢連接到離子應用程序?

user_model.php(coeigniter)

public function get_halal() 
{ 
    $term = "Buko,Beef,Sugar"; 
    $terms = explode(',', $term); 

    foreach($terms as $term){ 

    $this->db->select('*'); 
    $this->db->from('recipe'); 
    $this->db->join('menu', 'menu.recipe_id = recipe.recipe_id'); 
    $this->db->join('ingredient', 'ingredient.ingredient_id = menu.ingredient_id'); 
    $this->db->where('menu.category_id = 2'); 
    $this->db->like('ingredient.name', $term); 
    $query = $this->db->get()->result(); 
    } 
    return $query; 
} 

。我會怎麼做?

+1

解析'$ term'作爲參數? –

回答

0

當你想要創建一個函數(或一個類)時,首先你必須抽象出這個問題,以瞭解這個函數在不同的上下文中有多大用處。沒有規則,這取決於你的背景和你的目標。

在您的示例中,即該函數返回術語「Buko」,「Beef」和「Sugar」的數據庫查詢對象(事實上,僅適用於「Sugar」)。如果您以這種方式修改您的功能:

public function get_halal($what) 

您可以重新使用該功能來搜索任何條款。

如果您修改函數以這種方式:

public function get_halal($what) 
{ 
    if(is_array($what)) ... 
    else     ... 

您可以使用相同的功能來搜索一個或多個方面。

如果您修改函數以這種方式:

public function get_halal($what, $where) 

您可以搜索各種領域的術語。

有很多的可能性,這是最好的編碼樂趣。

相關問題