Select where the id of a previously selected is fetched

Asked

Viewed 74 times

0

I’m on a page where you’re printing out a menu. And underneath this data I want you to show me the ingredients of this menu, and I will search them from a list of ingredients.

Controller:

function lista_ingre()
{
    $this->load->model('ingredientes_model');
    $data['list'] = $this->ingredientes_model->getAllDisplayable_ing();
    //var_dump($data['list']); exit;
    $data['username'] = $this->session->userdata('username');
    $this->load->view('menus_detalhes_user',$data);
}   

Menu model:

function GetDisplayableByUsername_u($id)
{
    //indica os campos
    $this->db->select('id_menu, foto, nome, descricao, preco ');

    $result = $this->db->get_where('menus',array('id_menu' => $id));

    return $result->row();
}

Model of ingredients:

function getAllDisplayable_ing($id)
 {
    $this->db->select('id_ingrediente, lista_ingredientes.id_menu, nome_ingrediente, quantidade');
    $this->db->from('lista_ingredientes');
    $this->db->join('menus', 'menu.id_menu = lista_ingredientes.id_menu');

    $this->db->where('menu.id_menu', $id);
    $result = $this->db->get();
     return $result->result();

 }

Menu view:

     <p class="text-muted"><?php echo $menus->nome?></p>
 <p class="text-muted"><?php echo $menus->preco?>€</p>

View of the ingredients:

 <?php foreach ($list as $lista_igredientes): ?>
                <div class="service-box">
                    <img src="" class="img-responsive" alt="">
                    <h3><?php echo $lista_igredientes->nome_ingrediente?></h3>
                    <p class="text-muted"><?php echo $lista_igredientes->quantidade?></p>
                </div>
          <?php endforeach ?> 

Menu data appears well in the view, but ingredient data does not. The error is: Undefined variable: list and Invalid argument supplied for foreach()

Thank you

  • You are making a submenu menu from the database?

2 answers

-1

Apparently the variable $list is null. The reason for not bringing data from the database may be because the method getAllDisplayable_ing($id) of the ingredient model receives the parameter $id for the clause $this->db->where($id) and when you call this method in the controller you do not pass this value per parameter. Checks whether this value should be passed, if not, just remove the Where clause that receives the $id. :)

-1

I’d do it that way, see if it helps you:

function getAllDisplayable_ing($id=NULL)
 {

    $consuta = $this->db->get("lista_ingredientes")->result();
    foreach($consulta as &$valor){
        $this->db->where("id_menu",$valor->id_menu);
        $valor->submenu = $this->db->get("menus")->result();
    }
    return $consulta;
}

Passing the data to view:

$data['dados_lista'] = $this->ingredientes_model->getAllDisplayable_ing();

When you search for the data in the view:

foreach($dados_lista as $valor){
    echo $valor->nome_ingrediente."<br>"; // ingrediente
    foreach($dados_lista->submenu as $valor_sub){
        echo $valor_sub->menu; // subitem
    }
}

Browser other questions tagged

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