Welcome Guest, Not a member yet? Register   Sign In
Load data from Model to a View
#1

[eluser]Alfonso Martinez[/eluser]
I am trying to load the results form a query to a view directly. This is for display the user information in the header view. The header view is an include en all the view in my application.

This how I am trying:

perfil_model.php

Code:
class Perfil_model extends Model {  

    function headerUserInfo() {
        $query = $this->db->query('SELECT IdUsuarioWeb, Nombre FROM Usuarios_Web WHERE IdUsuarioWeb = 123');

        $data = null;

        if($query->num_rows() > 0) { // if results

            foreach ($query->result() as $row) {
                $data[] = $row;
            }
        }
        return $data;
    }
}

----

header.php

Code:
$this->load->model("perfil_model");
$data['perfil'] = $this->perfil_model->headerUserInfo();

echo $perfil->Nombre;


This, of course, is giving me an error.

What more can I do or try for that?
#2

[eluser]Ben Edmunds[/eluser]
If you insist on doing this the view code should be:

Code:
$this->load->model("perfil_model");
$perfil = $this->perfil_model->headerUserInfo();

echo $perfil->Nombre;

but you should not use a model in your view. You should interact with models in your controller and pass that to a view.
#3

[eluser]Alfonso Martinez[/eluser]
Thanks Ben,
I did that and I still get this error:

Quote:A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: includes/headerUserInfo.php
Line Number: 5

What other way could I accomplish what I need, without having to modify a lot of pages (controllers)? All I need is to display the info of the logged-in user on the header I have, but trying to display it on the view in that way (by calling the model directly) is giving me problems. Any suggestions?

Thanks!
#4

[eluser]Ben Edmunds[/eluser]
I would suggest using a MY_Controller to extend the Controller. Then all your controller will extend MY_Controller so you can process things on every page with MY_Controller and even include layouts. You can look it up on the wiki.

If you want to stick with doing what you are doing now post some more of your code and I'll see if I can't debug it for you.


Remember the code you have here:
Code:
$this->load->model("perfil_model");
$data['perfil'] = $this->perfil_model->headerUserInfo();

echo $perfil->Nombre;

Needs to be:
Code:
$this->load->model("perfil_model");
$perfil = $this->perfil_model->headerUserInfo();

echo $perfil->Nombre;
#5

[eluser]Alfonso Martinez[/eluser]
Thanks Ben,

Here is a more detailed code I have:

models/perfil_model.php
Code:
<?php

class Perfil_model extends Model {

    function headerUserInfo() {
        $query = $this->db->query('SELECT IdUsuarioWeb, Nombre FROM Usuarios_Web WHERE IdUsuarioWeb = '.$this->session->userdata('IdUsuarioWeb'));

        $data = null;

        if($query->num_rows() > 0) {

            foreach ($query->result() as $row) {
                $data[] = $row;
            }
        }
        return $data;
    }

}
?>

views/includes/header.php
Code:
<div id="header">
    <div id="row1">&lt;?php if($this->session->userdata('logged_in')==TRUE){ $this->load->view('includes/headerUserInfo'); } ?&gt;</div>
</div>

views/includes/headerUserInfo.php
Code:
&lt;?php
$this->load->model("perfil_model");
$perfil = $this->perfil_model->headerUserInfo();

echo $perfil->Nombre;
?&gt;

Having that and accessing the page while logged-ing I get this error message:
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$perfil_model
Filename: includes/headerUserInfo.php
Line Number: 3

Fatal error: Call to a member function headerUserInfo() on a non-object in /var/www/website/application/views/includes/headerUserInfo.php on line 3

Hope this helps to better debug my problem...

Thanks!
#6

[eluser]Ben Edmunds[/eluser]
Chnage your model to this:

Code:
&lt;?php

class Perfil_model extends Model {

    function headerUserInfo() {
        $query = $this->db->query('SELECT IdUsuarioWeb, Nombre FROM Usuarios_Web WHERE IdUsuarioWeb = '.$this->session->userdata('IdUsuarioWeb'));

        $data = null;

        if($query->num_rows() > 0) {

            return $query->result();
        }
        else {
            return false;
        }
    }

}
?&gt;

You were converting your object to an array with your foreach loop un-necessarily. If you want an array instead you can use $query->result_array().
#7

[eluser]Alfonso Martinez[/eluser]
Thanks again Ben,

I resolve the issue of the error message:
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$perfil_model
Filename: includes/headerUserInfo.php
Line Number: 3

Fatal error: Call to a member function headerUserInfo() on a non-object in /var/www/website/application/views/includes/headerUserInfo.php on line 3


By adding to config/autoload.php the perfil_model in the auto load models:
Code:
$autoload['model'] = array('perfil_model');

It seems to be that the line:
Code:
$this->load->model("perfil_model");
in headerUserInfo.php view was not loading the model correctly, but making it auto-load seems to work.


That was the way I made it work... however I'm not sure if this is reasonable way of accomplishing this. Would doing this cause any performance issues?

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB