foreach selective two tables

1

Please help me, I'm a beginner.

Well, I'm with a site with codeigniter, it's showing my recipes perfectly on:

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

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

Ineedittoretrievealltheid_receitaoftheloggedinuser,incase35,andtheaboveloopshowsallofthem,withoutshowingtheotherunsavedones.

Moreinformationifyouneedto:

CONTROLLER

publicfunctionfavoritos(){$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 multiple recipes saved as favorites, and I need to show only those saved, I do not want to show them all.

    
asked by anonymous 18.12.2018 / 14:42

1 answer

1

Well, I was able to follow the advice of @ 8biT, 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();        
}
    
18.12.2018 / 15:55