Codeigniter - Query in DB

Asked

Viewed 674 times

2

Good night!

I need to return the value of a table in codeigniter but it is acknowledging the error below:

PHP Error was encountered

Severity: Notice

Message: Undefined variable: name

Filename: paineladmin/categories.php

Line Number: 7

But my role in the model is correct, below:

public function get_bynome($nome=NULL){
    if($nome != NULL):
        $this->db->where('nome', $nome);
        $this->db->limit(1);
        return $this->db->get('categorias');
    else:
        return FALSE;
    endif;
}

And the call on the view is like this:

echo '<div class="small-12 columns">';
    echo breadcrumb();
    $query = $this->categorias->get_bynome($nome)->row();
    erros_validacao();
    get_msg('msgok');       
    echo form_open('categorias/cadastrar', array('class'=>'custom'));
    echo form_fieldset('Cadastrar nova categoria');
    echo form_label('Nome');
    echo '<div class="row">';
    echo '<div class="small-5 columns">';
    echo form_input(array('name'=>'nome'), set_value('nome'), 'autofocus');
    echo '</div>';
    echo '</div>';
    echo form_label('Categoria Pai');
    echo '<div class="row">';
    echo '<div class="small-5 columns">';
    echo form_dropdown('Categoria Pai', $query->nome, 'Selecione uma opção');
    echo '</div>';
    echo '</div>';



public function cadastrar(){
  //esta_logado();
  $data['query'] = $this->categorias->get_bynome($nome)->row();    
  $this->load->view("categorias", $data);

It seems that the variable is not declared something like this, I did not find the error.

  • Which of the 2 is the paineladmin/categories.php file?

  • The view, the bottom

  • I don’t know how the CI renders the view... $query = $this->categorias->get_bynome($nome)->row() the view is receiving the right $name variable? From a var_dump( $nome ) at the beginning of the view.

  • Yes, the view is receiving the $name variable of the get_byname function of the model. No use with var_dump($name) hehe

  • Could put the code that calls the view?

  • 1

    public Function register(){ //esta_logated(); $data['query'] = $this->categories->get_byname($name)->Row(); $this->load->view("categories", $data);

  • If $nome comes from a POST/GET, you should recover like this: $nome = $this->input->post('nome'); that in the cadastrar().

Show 2 more comments

2 answers

2

From what I could see, this error is because you are not passing the variable to the view, this should be done from your controller, and I don’t know if I’m right, but this part is from your correct controller:

public function cadastrar(){
  //esta_logado();
  $data['query'] = $this->categorias->get_bynome($nome)->row();    
  $this->load->view("categorias", $data);

if yes, you should have done so

public function cadastrar(){
  //esta_logado();
  $data['query'] = $this->categorias->get_bynome($nome)->row();    
  $data['nome'] = $nome; //passar a variavel nome para a view
  $this->load->view("categorias", $data);

by the time you asked the question you should have already solved, but only confirms if it was that.

0

NOTICE type errors in PHP can be ignored by calling the following function at the beginning of the file:

error_reporting(E_ALL ^ E_NOTICE);

In Codeigniter, this function is called in the index.php file. You can check between lines 31 and 47 in the framework source code:

https://github.com/EllisLab/CodeIgniter/blob/2.2.0/index.php

Browser other questions tagged

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