1

ingredient - ingredient

menu - menu enter image description here recipe

console

How will I merge the two array under name into one array and make it unique. As you can see the result on the [0] is

Beef Adobo

qwerty

iswi

and on 1 is

qwerty

iswi

I want the both of them to be in one array and the result should be

Beef Adobo

qwerty

iswi

query:

public function get_halal($name) {

    $terms = explode(',', $name);

    foreach ($terms as $name) {
        $this->db->distinct();
        $this->db->select('r_name');

        $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', $name);

        $query = $this->db->get()->result();

        $data[] = $query;
    }
    return $data;
}

controller:

public function ajaxSearchHalal() {
    postdata = file_get_contents("php://input");

    if (isset($postdata)) {
        $post = json_decode($postdata);

        $name = $post->name;


        if ($this->user_model->get_halal($name)) {
            $user_id = $this->user_model->get_halal($name);

            $data = array(
                'name' => $user_id,
            );

            echo json_encode($data);
        } else {
            echo json_encode("false");
        }


    } else {
        echo "Error!";
    }
}
4
  • what is the purpose of your menu table exactly? Commented Jan 31, 2016 at 12:05
  • Can you show an example of your menu table in the database? Commented Jan 31, 2016 at 12:31
  • 1
    The reason Im asking about the menu table is because you shouldn't have to worry about merging your result arrays. You just need to fix your query. Provide your tables with examples so that we can give you the best answer. Commented Jan 31, 2016 at 12:35
  • @CodeGodie the menu table is where i can get the category_id of my recipes Commented Jan 31, 2016 at 22:46

1 Answer 1

2

Ok, I see what you are doing now. Thanks for including your tables.

From what I see, you are trying to get all the recipe names, from the recipe table, that have the ingredients your are passing.

What you need to do to fix this issue is not worry about how to merge the arrays, but how you can redo your sql query in order to obtain the information you want in just one query. The way you have it now is not efficient as you are calling a query per every iteration.

What you need to do is use WHERE IN, and GROUP BY to get the info you need and group them by a column. Redo your model method like this:

public function get_halal($name) {

    $terms = explode(',', $name);

    $this->db->select("r.name");
    $this->db->from('recipe r');
    $this->db->join('menu m', 'm.recipe_id = r.recipe_id');
    $this->db->join('ingredient i', 'i.ingredient_id = m.ingredient_id');
    $this->db->where('m.category_id = 2');
    $this->db->where_in('i.name', $terms);
    $this->db->group_by('r.recipe_id');
    $q = $this->db->get();
    return $q->result();
}

This will give you one result set which you can then pass as JSON to your front-end without having to iterate or merge arrays.

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Sweet. No problem. Glad I was able to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.