0

I'm new to MVC patterns and OOP in general.

I have an application in CodeIgniter, and the basic part works. There is a form that asks for a username and password. If they match the database, the user is logged in, a session is created and they are redirected to another part of the site. This all works fine.

I now want to add two more functions, one updateloggedonuser is to update the database when the user is logged in, set a bit field to 1 and set a timestamp.

The other is getusersname. This one I want to get the logged on user's firstname, lastname and ID from the database and return them so I can store them in a session variable.

I am getting a Fatal error: Call to undefined function getusersname().

Controller:

class Login_c extends CI_Controller {

    function index (){
        $view['main_content'] = 'loginform_view';
        $this->load->view('includes/template', $view);
    }

    function validate_credentials() {
        $this->load->model('login_m');
        $query = $this->login_m->validate();
        if($query) // if the users credentials valid
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
                );

            $this->session->set_userdata($data);
            redirect('site_c/main_area');
        }
        else
        {
            $this->index();
        }
    }

    function logout(){
        $this->index();
        $this->session->sess_destroy();
    }

}

Model:

class Login_m extends CI_Model {

    function validate(){
        $this->db->where('UserName', $this->input->post('username'));
        $this->db->where('UserPassword', md5($this->input->post('password')));
        $query = $this->db->get('User');
        $userN = $this->input->post('username');
        if($query->num_rows == 1) 
        {
            getusersname($UserN);
            updateloggedonuser($UserN);
            return true;    
        }
    }


    function updateloggedonuser($UserN){
        // with username set isActive to 1 and Updatedate to update 'datetime'
        $date = new DateTime();
        $data = array('isActive' =>1,
                    'UpdateDate' => $date);
        $this->db->where('UserName', $UserN);
        $this->db->update('User',$data);    
    }

    function getusersname($UserN){
        // with username get FirstName , LastName and UserID from Database 
        $sql = "SELECT UserID, FirstName, LastName FROM User WHERE UserName =?";
        $q= $this->db->query($sql,$UserN);
        if($q->num_rows() ==1){
            foreach ($q->result() as $row) {
                $userdata[] = $row;
            }
            return $userdata;
        }
    }

}

I think you can see what I'm trying to do, but obviously I'm not doing it the right way.

Spudley
  • 166,037
  • 39
  • 233
  • 307

3 Answers3

1

Try like this

$this->my_function_name();

So they would be

if($query->num_rows == 1) 
{
    $this->getusersname($UserN);
    $this->updateloggedonuser($UserN);
    return true;    
}

$this actually represents the singleton Codeigniter instance which is actually the controller object.

GautamD31
  • 28,552
  • 10
  • 64
  • 85
1

In the second modal example, you need to change getusername() to $this->getusername();

function validate(){

    $this->db->where('UserName', $this->input->post('username'));
    $this->db->where('UserPassword', md5($this->input->post('password')));
    $query = $this->db->get('User'); 

    $userN = $this->input->post('username');
    if($query->num_rows == 1) 
    {
    $this->getusersname($UserN);
    $this->updateloggedonuser($UserN);
    return true;    

    }

}
Phil Cross
  • 9,017
  • 12
  • 50
  • 84
1

You want to call the method, therefore you need to add $this-> to the method call:

$this->getusersname($UserN);

In quite a few programming languages a function call within a class method is interpreted as a method call. In PHP you have always to specify you want to call a method with $this-> otherwise he will look at the global function namespace.

Shoe
  • 74,840
  • 36
  • 166
  • 272