Association between 2 tables

Asked

Viewed 85 times

2

I’m having a problem retrieving data from two tables, both associated. When I go in the browser and put the controller and then the method, it comes to me with this message Array(). Could someone tell me the problem with my code?

    public function categoria($slug_categoria = null){

    //Recebendo os dados das categorias
    $menu_categoria['categorias'] = $this->db->get('categorias')->result();

    //Criando querys SQL com JOIN usando o Active Record
    $this->db->select('r.id_receita, r.nome, r.slug_receita, r.foto, c.categoria');
    $this->db->from('receitas r');
    $this->db->join('categorias c', 'c.id_categoria = r.categoria', 'INNER');
    $this->db->where('c.slug_categoria', $slug_categoria);
    $this->db->order_by('r.nome', 'ASC');

    $receita['receitas'] = $this->db->get()->result();

    //Carregando as views 
    $this->load->view('html_header');
    $this->load->view('header');
    $this->load->view('menu_category', $menu_categoria);
    $this->load->view('categoria', $receita);
    $this->load->view('footer');
    $this->load->view('html_footer');       
}

I tried this:

<?php
echo heading("Receitas Deliciosas", 2);
if (count($receitas) > 0) {
    ?>
    <ul>
        <?php
        echo heading($receitas[0]->categoria, 3);
        foreach ($receitas as $item):
            ?>
            <li class="gradiente1 radious">
                <?php
                echo anchor("receita/" . $item->slug_receita, $item->nome);
                ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php
}else {
    echo "Nenhuma receita encontrada nesta categoria.";
}
?>

I looped it and it ran echo "No recipes found...".

  • Manage to solve the problem, thanks for the help

1 answer

1

This is because you are echo an array variable. Since it is not "printable", php plays on the screen the "type of it".

In the view, instead of

echo $receitas

Place

var_dump( $receitas );

And you’ll see your recipes. Obviously, this is just for debugging. To list recipes, you will need to iterate in a loop, using foreach, for example.

  • 1

    Hello, thanks for the help. I did what you asked, then he returned it to me from the array variable (size=0) Empty. He knows how to tell me what can be?

  • This means that the variable is being populated with an empty array. That is, your SQL is not bringing anything.

Browser other questions tagged

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