message Undefined offset 0 no codeigniter

Asked

Viewed 253 times

0

I have a problem to set the data in a view with the Codeigniter framework.

Model:

// Metodo que busca os dados de um cliente especifico
public function getCliente($id){
    $this->db->select('*');
    $this->db->from($this->table);
    $this->db->limit(1);
    $this->db->where($this->primaryKey, $id);
    return $this->db->get()->result_array()[0];
}

Controller:

// Verifica se o id é nulo
if ($id != null){
   // Decodifica o id passado
   $id = decode($id);
   // Busca os dados do cliente especifico
   $data['cliente'] = $this->Clientes_model->getCliente($id);
} else {
  // Não busca dados
  $data['cliente'] = null;
}

View:

<div class="form-group">
    <label class="col-sm-2 control-label">Nome *</label>
    <div class="col-sm-10"><input type="text" class="form-control" required name="nome_cliente" value="<?php echo $cliente['nome_cliente']; ?>">
    </div>
</div>

but I’m returning the error:

message Undefined offset 0

have already given a var_dump and there is that position in array, I don’t know why I made a mistake?

  • Maybe the error isn’t ai! on which line is the error? and read in your view to see where the error stops!

  • the error is on this line: Return $this->db->get()->result_array()[0]; the error does not stop, always generating new error until the browser crashes

  • 1

    on that line do return $this->db->row_array(); (https://www.codeigniter.com/userguide3/database/results.html) it will return only one line, and that’s what it needs! and make sure after that line is returning something.

  • could display, now it keeps repeating the view, while arrow the value of the fields

  • 1

    It’s something you’re doing... !!! read the Codeigniter manual and it’s simple http://www.codeigniter.com/user_guide/

No answers

Browser other questions tagged

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