Nurullah Macun
Nurullah Macun

Reputation: 552

Codeigniter can't load model

controllers/verify_login.php page

   <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Verify_login extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('adminl_model') ;
 }

 function index()
 {
   //This method will have the credentials validation
   $this->load->library('form_validation');

   $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
   $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

   if($this->form_validation->run() == FALSE)
   {
     //Field validation failed.  User redirected to login page
     $this->load->view('admin/index');
   }
   else
   {
     //Go to private area
     redirect('about', 'refresh');
   }

 }

 function check_database($password)
 {
   //Field validation succeeded.  Validate against database
   $username = $this->input->post('username');

   //query the database

   $result = $this->Admin_Model->login($username, $password);

   if($result)
   {
     $sess_array = array();
     foreach($result as $row)
     {
       $sess_array = array(
         'id' => $row->id,
         'username' => $row->username
       );
       $this->session->set_userdata('logged_in', $sess_array);
     }
     return TRUE;
   }
   else
   {
     $this->form_validation->set_message('check_database', 'Invalid username or password');
     return false;
   }
 }
}

models/adminl_model.php page

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Adminl_Model extends CI_Model
{
     function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
 function login($username, $password)
 {
   $this -> db -> select('id, username, password','type');
   $this -> db -> from('users');
   $this -> db -> where('nick', $username);
   $this -> db -> where('type', 'admin');
   $this -> db -> where('pass', MD5($password));
   $this -> db -> limit(1);

   $query = $this -> db -> get();

   if($query -> num_rows() == 1)
   {
     return $query->result();
   }
   else
   {
     return false;
   }
 }

}

Gives error. How can I fix that ? I am working on c9.io. and stackoverflow saying that is so codely. I understood my problem that is ridiculous.

Upvotes: 1

Views: 575

Answers (2)

Hatem
Hatem

Reputation: 369

This chunk of code :

$result = $this->Admin_Model->login($username, $password);

Must be:

$result = $this->adminl_model->login($username, $password);

Upvotes: 1

abdullacm
abdullacm

Reputation: 636

use $this->admin_model->login($username, $password); instead of $this->Admin_Model->login($username, $password);

Once loaded, you will access your model functions using an object with the same name as your class:

here you have loaded the model as $this->load->model('adminl_model') ;, so u can use adminl_model instead of Admin_Model

Upvotes: 1

Related Questions