Codeigniter - cannot print select values in view

Asked

Viewed 208 times

1

Good, I’m trying to make a page that shows the user data that is logged in.

Model:

function getAllDisplayable3()
 {
     $username = $this->session->userdata('username');
     $this->db->select('id_login, nome, username, password, cod_postal, telefone, email, localidade, rua');
     $this->db->from('login');
     $this->db->where('username', $username);
     $result = $this->db->get();
     //echo $username; echo die();
 }

I did the $username echo and printed.

<input class="form-control" id="nome" value="<?php echo $perfil->nome?>" type="text">

And gives error: Undefined variable: profile. What have to put to print values?

Thank you.

  • Missing the Return in function

  • @Andre Bail I already put Return at the end of the function: return $result->result();. Yet I still have the same problem.

1 answer

2

Your data needs to be passed through the controller first and then passed to the view.

Model

function getAllDisplayable3() {
    $username = $this->session->userdata('username');
    $this->db->select('id_login, nome, username, password, cod_postal, telefone, email, localidade, rua');
    $this->db->from('login');
    $this->db->where('username', $username);
    return $this->db->get();
}

Controller

class Formulario extends CI_Controller {

    public function index()
    {
        $this->load->model('perfil'); // Nome do model

        // Faz a chamada da função
        $dados = $this->perfil->getAllDisplayable3(); 

        // Envia os dados recebidos para a view
        $this->load->view('formulario', $dados);
    }
}

View

<input class="form-control" id="nome" value="<?php echo $perfil; ?>" type="text">
  • Good @Vinicios, I have controller, but as I think it is not important for the problem I did not put. My controller is the following: function perfil()&#xA; {&#xA; $this->load->model('perfil_model');&#xA; $data['list'] = $this->perfil_model->getAllDisplayable3();&#xA; $data['username'] = $this->session->userdata('username');&#xA; $this->load->view('perfil_view',$data);&#xA; }

  • after creating the variable "$data['list']" use a "var_dump($data['list']); Exit;" and check the returned value.

  • @Vinicious Yals returned me an array with all values array(1) { [0]=> object(stdClass)#21 (9) { ["id_login"]=> string(2) "23" ["nome"]=> string(4) "lola" ["username"]=> string(3) "lol" ["password"]=> string(3) "lol" ["cod_postal"]=> NULL ["telefone"]=> NULL ["email"]=> NULL ["localidade"]=> NULL ["rua"]=> NULL } }

  • @Viniciuous Yals , already solved the problem. It was in the view, it lacked the foreach. Thank you

Browser other questions tagged

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