1
votes

I have 3 tables the ingredient(ingredient_id,name),menu(recipe_id,ingredient_id,category_id) and recipe(recipe_id, r_name,r_description).

ingredient_id name

1 Onion

2 Paul
3 Pepper

4 Oil

recipe_id ingredient_id category_id

1 3 1

1 4 1

2 3 1

2 4 1

recipe_id r_name r_description

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();

}
3
Are you using Active Record? Do you have any code that you've tried but isn't quite right?Windle
Yes @Windle I'm using active record. I've edited my question. please take a look at it.ThanksMs. A

3 Answers

1
votes

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.

0
votes

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();

See the guide of codeigniter

0
votes
$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