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.