Welcome Guest, Not a member yet? Register   Sign In
Retreiving Values from a Model
#1

[eluser]ufasoli[/eluser]
Hello all,
I've been trying to recover values from a model after beign initialised.

Here is how the application works

I've got a controller named "Account" which loads a view in which the user identifies himself. When he clicks on the Ok button a function "checkLogin" is called(by an Ajax call) to verify his credentials if the username and password are correct, the "init" function of the model is called and all the model variables are initialised with the user's information from the database. After this is done the user gets redirected to a new page(function "userAccount") where I would like to display all this information, that's why I pass "$this->user" as argument on the "$this->load->view('user', $this->user)", but all the variables are now empty...
So my question is: is it possible to keep the values of the model once they have been initialised? or do I have to call every time the 'init' method of the Model?? I have tried initialising the variables within the model's constructor, but the problem is still there.. I know as well that this could be done with session variables but I'd rather not do it that way.

here is my controller

Code:
<?php


class Account extends Controller
{
    function __construct()
    {
        
        parent::Controller();
        $this->load->helper('url');
        $this->load->database();
        $this->load->model('User', 'user', true);

    
    }

    function index()
    {    
        

        $this->load->view('logIn');

        
    }
    
    function checkLogin()
    {
        
        
        
        $login = $_POST['login'];
        $pwd = $_POST['pwd'];
        
        
              $query = $this->db->getwhere('utilisateurs', array('userid' => $login, 'pwd'=> $pwd));
        
        
        
        if($query->num_rows() != 1)
        {
                $msgLogin = "<h3 style=\"color: #663300;\"> Erreur de Connexion : </h3> <h4> Votre identifiant ou votre mot de passe est incorrect. </h4>";
    
            echo $msgLogin;

        }
        else
        {
            $query->free_result();
            $this->user->_init($_POST['login'], $_POST['pwd']);
            
    echo $this->ajax->tag("location.href ='".base_url()."Account/userAccount'");
            
                
            
        }
        
        
    }
    
    function userAccount()
    {
        $this->load->view('user', $this->user);
    }
    
        
}    
    
?&gt;
and here is my model

Code:
&lt;?php


class User extends Model
{

var $userid = "";
var $lastname = "";
var $name = "";


    
    function __construct()
    {
        parent::Model();
            
    
    }
    
    function _init($userid, $pwd)
    {
        
        $userInfo = $this->db->getwhere('utilisateurs', array('userid'=>$userid, 'pwd'=>$pwd));
        
        if($userInfo->num_rows() != 1)
        {
            return ;
        }
        else
        {
            foreach ($userInfo->result_array() as $row)
            {
                $this->userid = $row['userid'];
                $this->firstname = $row['firstname'];
                $this->lastname = $row['lastname'];
                
                
            }
            
            $userInfo->free_result();
        }
            
        
        
    }
    
    

    
}



?&gt;

Thanks in advance and sorry for my bad english

Ulises
#2

[eluser]gtech[/eluser]
I had a similar problem and cheated by using the cookie helper (it maintained variable state after closing the browser) it worked fine however I am not sure how you would maintain the variables from one request to another without either using the session class or cookie helpers.
#3

[eluser]xwero[/eluser]
The problem is not not model or controller but keeping the data alive. The user object in the userAccount function doesn't exist after the redirect.

Using ajax makes is more difficult because after a correct login you have tw options :
- redirect to a page where you set a session variable and then you redirect to the userAccount function, check if the session variable has the right content.
- you could set a cookie in your checkLogin function and in the userAccount function you check if the cookie has the right content.

After being sure the session/cookie has the correct value you can extract the userdata from the database.
#4

[eluser]ufasoli[/eluser]
That's what I've thought at first, but I said maybe there is a way... Well thank you very much for your answers..




Theme © iAndrew 2016 - Forum software by © MyBB