I can’t make select

Asked

Viewed 51 times

3

Model:

public function exibir_noticia()
{  

  $consulta = $this->db->query('SELECT * FROM Noticia_Site_Cairu');
  return $consulta->result();
}

Controller

public function index()

{
    $this->load->model('noticia_model');
    $consulta = $this->noticia_model->exibir_noticia();         
    //print_r($casdatrado);
    $this->load->view('v_home', $consulta);
}

When I go to show in the view:

                foreach ($consulta as $exibir):{            
                echo '                  
                <tr class="success">
                    <td>'.$exibir->titulo.'</td>
                    <td>'.$exibir->texto.'</td>
                    <td>'.$exibir->imagem.'</td>
                    <td>'.$exibir->link.'</td>
                    <td>'.$exibir->nome_link.'</td>
                    <td>'.$exibir->video.'</td>
                    <td>'.$exibir->data.'</td>
                </tr>';
                }endforeach;

This error appears:

PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: views/v_home.php

Line Number: 88

Backtrace:

File: /var/www/html/admnoticias/application/views/v_home.php Line: 88 Function: _error_handler

File: /var/www/html/admnoticias/application/controllers/Home.php Line: 17 Function: view

File: /var/www/html/admnoticias/index.php Line: 315 Function: require_once

1 answer

3


$consulta does not exist in the view because it was not defined, see the controller code:

$this->load->view('v_home', $consulta);

To set the name of the variable that will be manipulated in the view create an associative array:

$this->load->view('v_home', array('consulta' => $consulta));
  • Thanks worked! Always have to put an array??? I thought just sending the variable was enough. Thank you very much!

  • @web_charles yes always, this is common. See that in your example you passed only one variable, and if they were more like you would do? an array would no longer be practical? :)

  • is true! Thanks for the tip ;)

  • @web_charles vc can mark one of the answers as 'accepted' (green light) it means one of them solved your problem. You can see more details on: How and why to accept an answer?.

Browser other questions tagged

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