Welcome Guest, Not a member yet? Register   Sign In
Making instances of models
#1

[eluser]Alexander Radsby[/eluser]
&I;have this problem, if a person isn't logged in he will redirected to a login page.
When you enter the username & password and click submit, you'll get redirected to a checklogin funktion.

The thing is that I want to make an instance of the user model, set the instance variabels and then just check if they exist by echoing them. But when I do this I get nothing. Nothing happens.

Can someone explain this for me? The whole thing is really simple actually, I need this to work so I can continue with CI Smile

Check the code for the function checklogin and the user model. I commented out the code so you can focus on the code in the function checklogin and in the user model.

Thanks!

Login Controller
Code:
<?php
class Login extends Controller {
/*
    function Login()
    {
        parent::Controller();
        $this->load->library('session');
        $this->load->library('validation');
        $this->load->helper(array('form', 'url'));
    }
    

    function index()
    {    
        if(!$this-;session->userdata('authenticated')) {
            $this->load->view('admin/login_view');
        }
        else
        {
            redirect('admin/dashboard');
        }
    }
*/
    function checklogin()
    {
        
        $name = $this->input->post('username');
        $password = md5($this->input->post('password'));
        
        $this->load->model('user_model');
        
        $this->user_model->set_name($name);
        $this->user_model->set_password($password);
        
        echo $this->user_model->get_name();
        echo $this->user_model->get_password();
        
    }
    
}

User Model
Code:
<?php
class User_model extends Model {
    
   var $name = '';
   var $password = '';
    
    function User_model()
    {
        parent::Model();
    }
    
    function set_name($name)
    {
        $this->name = $name;
    }
    
    function set_password($password)
    {
        $this->password = $password;
    }
    
    function get_name()
    {
        return $this->name;
    ]
    
    function get_password()
    {
        return $this->password;
    }
    
}
?>

*edit took away the wrongly formatted () in the model
#2

[eluser]wiredesignz[/eluser]
class User_model() extends Model() should be: class User_model extends Model
Same format as the controller declaration above.
#3

[eluser]Alexander Radsby[/eluser]
[quote author="wiredesignz" date="1201111127"]class User_model() extends Model() should be: class User_model extends Model
Same format as the controller declaration above.[/quote]

Opps. Sorry my mistake. But I still don't get any result from the echo in the controller.
#4

[eluser]wiredesignz[/eluser]
The controller constructor is commented out, your controller can't connect to CI and therefore can't see the Model.
#5

[eluser]Alexander Radsby[/eluser]
[quote author="wiredesignz" date="1201112130"]The controller constructor is commented out, your controller can't connect to CI and therefore can't see the Model.[/quote]

I just commented it out so people would focus on the function checklogin and the user_model.
#6

[eluser]wiredesignz[/eluser]
Well there isn't much wrong with the Model class as shown. Your problem is elsewhere.

Post the ACTUAL CODE, your edited version is misleading.

Can you post the output from: echo var_dump$this->user_model); insert it as the last line of your check_login function.
#7

[eluser]Alexander Radsby[/eluser]
Alright here's the full code for the project. As requested.

Login Controller (login.php)
Code:
<?php
class Login extends Controller {

    function Login()
    {
        parent::Controller();
        $this->load->library('session');
        $this->load->library('validation');
        $this->load->helper(array('form', 'url'));
    }
    

    function index()
    {    
        if(!$this->session->userdata('authenticated')) {
            $this->load->view('admin/login_view');
        }
        else
        {
            redirect('admin/dashboard');
        }
    }
    
    function checklogin()
    {
        
        $name = $this->input->post('username');
        $password = md5($this->input->post('password'));
        
        //echo $name;
        //echo $password;
        
        $this->load->model('user_model');
        
        $this->user_model->set_name($name);
        $this->user_model->set_password($password);
        
        echo $this->user_model->get_name();
        echo $this->user_model->get_password();
        
    }
    
}
?>

User Model (user_model.php)
Code:
<?php
class User_model extends Model {
    
    var $name = '';
    var $password = '';
    
    function User_model()
    {
        parent::Model();
    }
    
    function set_name($name)
    {
        $this->name = $name;
    }
    
    function set_password($password)
    {
        $this->password = $password;
    }
    
    function get_name()
    {
        return $this->name;
    ]
    
    function get_password()
    {
        return $this->password;
    }
    
}
?>

The Login View (login_view.php)
Code:
<?php $this->load->helper('form'); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"&gt;
&lt;head&gt;
  &lt;title&gt;Login&lt;/title&gt;

&lt;/head&gt;
&lt;body&gt;
    <h1>Login</h1>
    
    &lt;?php if($this->validation->error_string): ?&gt;
    <ul class="error">
        &lt;?=$this->validation->error_string; ?&gt;
    </ul>
    &lt;?php endif ?&gt;

    &lt;?= form_open('login/checklogin'); ?&gt;
        <label for="username">Username</label>
        &lt;?= form_input('username'); ?&gt;
        <label for="password">Password</label>
        &lt;?= form_password('password'); ?&gt;
        &lt;?= form_submit('submit','Login'); ?&gt;
    &lt;?= form_close(); ?&gt;
    
&lt;/body&gt;
&lt;/html&gt;
#8

[eluser]Alexander Radsby[/eluser]
[quote author="wiredesignz" date="1201112534"]Well there isn't much wrong with the Model class as shown. Your problem is elsewhere.

Post the ACTUAL CODE, your edited version is misleading.

Can you post the output from: echo var_dump$this->user_model); insert it as the last line of your check_login function.[/quote]

I don't get any output at all. It's supposed to be like this right: echo var_dump($this->user_model); ?
#9

[eluser]wiredesignz[/eluser]
Yes.

Your code looks ok, apart from loading the form helper in the View which is bad practice (its already loaded in the controller anyway).

I think you have other issues.
#10

[eluser]Alexander Radsby[/eluser]
[quote author="wiredesignz" date="1201114090"]Yes.

Your code looks ok, apart from loading the form helper in the View which is bad practice (its already loaded in the controller anyway).

I think you have other issues.[/quote]

Hmm I'll try to redo the whole project, I was just starting out anyway. Thanks a lot of the help mate Smile




Theme © iAndrew 2016 - Forum software by © MyBB