Undefined variable when sending data from the controller to the view

0

I'm doing a small practice project where I take out information from the database and show it in a view as a "profile" type.

At the moment of showing it, I get an indefinite variable error, but that variable is already brought in the query

This is the method of the user model that brings the information from the database:

function datosUsuario($id){
        $this->db->select('nombre, apellido, direccion, telefono, correo');
        $this->db->where('id_usuario', $id);
        //$this->db->limit(1);
        $query = $this->db->get('tbl_usuarios');
        return $query->result_array();
    }

This is the view:

<h1>Bienvenido&nbsp;<span id="usuario">@<?= $id; ?></span></h1>
<div class="container">
  <div class="row">
    <div class="col-lg-6 col-md-6 col-sm-1">
      <label for="nombre">Nombre:</label>
    <p><?= $nombre; ?></p>
  </div>
</div>
</div>

and finally, this fragment, which is the function of the controller:

function index(){

    if($this->session->userdata('login_ok'))
    {
        $data = $this->usuario->datosUsuario((string)$this->session->userdata('id_usuario'));
        $data['id'] = $this->session->userdata('id_usuario');
        $this->load->view("perfil", $data);
    }else{
        Redirect('inicio/index', 'location');
    }
}
    
asked by malditagaseosa 16.11.2017 в 07:11
source

1 answer

0

Analyzing your question, there is a lack of management on how to send the information from the controller to the view.

The model would be advisable to stir an object, it is easier to handle in the view. I leave the link with more information .

function datosUsuario($id){
    $this->db->select('nombre, apellido, direccion, telefono, correo');
    $this->db->where('id_usuario', $id);
    //$this->db->limit(1);
    $query = $this->db->get('tbl_usuarios');
    return $query->result();
}

Then comes the controller, where in your code it is not appreciated if in the driver's constructor you load the model where the function is located and in how you try to pass the data that the model returns to the controller.

function __construct()
{
        parent::__construct();
        //Suponiendo que se llame asi tu modelo
        $this->load->model('usuario');
}

function index()
{
  if($this->session->userdata('login_ok'))
  {
     //Primero guardar la id_usuario en una variable
     $id_usuario = $this->session->userdata('id_usuario');
     //Aqui esta el primer detalle, tienes que asignar un Indice para después recuperar la información en la vista
     $data['info_usuarios'] = $this->usuario->datosUsuario($id_usuario);
     $data['id'] = $id_usuario;
     $this->load->view("perfil", $data);
  }
  else
  {
     Redirect('inicio/index', 'location');
  }
}

To end with the view, where it would be something like this

 <h1>Bienvenido&nbsp;<span id="usuario">@<?= $id ?></span></h1>
 <div class="container">
     <div class="row">
         <div class="col-lg-6 col-md-6 col-sm-1">
              <label for="nombre">Nombre:</label>
              <p><?= $info_usuarios->nombre ?></p>
         </div>
     </div>
  </div>

If you analyze the view, the id is used that way since it is only one data that you pass, whereas the other one is a Object , and you must go through it that way. I hope my answer is helpful:)

    
answered by 27.11.2017 / 20:31
source