Reputation: 45
I have 3 tables the ingredient(ingredient_id,name),menu(recipe_id,ingredient_id,category_id) and recipe(recipe_id, r_name,r_description).
1 Onion
2 Paul
3 Pepper
4 Oil
1 3 1
1 4 1
2 3 1
2 4 1
1 Adobo yummy
2 Kaldereta yucky
What I want is if I search "Pepper, Oil" , the result should be the recipe that corresponds with the ingredients which is the adobo and kaldereta.
I had this tried but it is only static i want it dynamic and also i want to explode ingredient_name so that I could search multiple words.
public function get_halal()
{
$this->db->distinct();
$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 = 1');
$this->db->like('ingredient.ingredient_name','Mango', 'both');
$query = $this->db->get();
return $query->result();
}
Upvotes: 1
Views: 1678
Reputation: 1145
If i understood what you want to do, give a try to this. You need to set your $search_values, you have a string or an array ? Supouse you have a string:
//$search_values = "Pepper, Oil";
public function get_halal($search_values)
{
$this->db->distinct();
$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 = 1');
if (strpos($search_values, ',') !== false) {
$search = explode(',' , $search_values);
$this->db->like('ingredient.ingredient_name', trim($search[0]), 'both');
unset($search[0]);
foreach ($search as $term){
$this->db->or_like('ingredient.ingredient_name', trim($term), 'both');
}
}else{
//this means you only have one value
$this->db->like('ingredient.ingredient_name',$search_values, 'both');
}
$query = $this->db->get();
return $query->result();
}
in case $search_values is an array you have to skip to the if condition directly and do the foreach.
Upvotes: 1
Reputation: 660
$this->db->select('recipe.r_name');
$this->db->from('recipe');
$this->db->join('menu', 'recipe.recipe_id = menu.recipe_id');
$this->db->join('ingredient' , 'ingredient.ingredient_id = menu.ingredient_id');
$this->db->where('ingredient.ingredient_name in (your Search Values));
$this->db->get();
Try This Code .this code Work fine for me
Upvotes: 0
Reputation: 116
First u have to make the query so you can find the selected data which u have search
$this->db->select('recipe.r_name');
$this->db->from('recipe');
$this->db->join('menu', 'recipe.recipe_id = menu.recipe_id');
$this->db->join('ingredient' , 'ingredient.ingredient_id = menu.ingredient_id');
$this->db->where('ingredient.ingredient_id' , your search id);
$this->db->get();
Upvotes: 0