2016-01-29 50 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) 

您可以搜索各种领域的术语。

有很多的可能性,这是最好的编码乐趣。

相关问题