Selective foreach two tables

Asked

Viewed 287 times

1

Please help me, I’m a beginner.

Well, I’m with a website with codeigniter, it’s showing my recipes perfectly on:

<?php foreach ($receitas as $receita) :?>

Now I want to create a loop that only shows the recipes that are in the favorites, which in this case is another table.

inserir a descrição da imagem aqui

I need it to recover all the logged-in user’s id_recipe, in case 35, and the above loop shows all of them, without showing the other unsaved ones.

more information if you need:

CONTROLLER

public function favoritos(){

    $this->load->model("Receitas_model");
    $lista = $this->Receitas_model->buscaTodos();
    $dados = array ("receitas" => $lista);
    $this->load->view('templates/header');
    $this->load->view('receitas/favoritos', $dados);
    $this->load->view('templates/footer');

}

MODEL

public function buscaTodos(){
    $this->db->order_by('criado','desc');
    return  $this->db->get("receitas")->result_array();     
}

The logged in user can have several recipes saved as favorite, and need to show only those saved, do not want to show all.

  • Welcome to @Gabriel Ichikawa Craice! Add your question more info about the other table favoritos, and also what you have tried to solve the problem. But in advance, with the information you have put, it seems that only you should add a INNER JOIN in your consultation lynching your table favoritos.

  • Hello @8bit , first thank you for the reply. I put the format image of the bookmarked table, it contains only " ID (int) , id_usuario(int) , id_recipe(int) " it only stores the information of when a user adds a certain recipe to the bookmarks, obviously the same user can have several saved recipes, and need to show this, all recipes saved by user X.

1 answer

1

Well, I managed to follow the @8bit advice, code below:

public function buscaTodos3(){
    $usuarioID = $this->session->userdata('usuario_logado')['id'];

    $query = "SELECT * FROM receitas 
    INNER JOIN favoritos 
    ON favoritos.id_receita = receitas.id
    WHERE id_usuario = '$usuarioID'";

    $result = $this->db->query($query);
    return  $result->result_array();        
}
  • Great, glad you made it!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.