-2

i try to use a function but face a problem. I make research on the Net but there's no a solution

i have a model . You can see below:

   <?php

class kayitmodel extends CI_Model {

    function User_model() {
        parent::Model();
    }

    function uyeEkle($username, $email, $password, $activationCode) {
        $sha1_password = sha1($password);

        $query = "insert into pasaj_register(username,email,password,activationCode) values(?,?,?,?)";
        $this->db->query($query, array($username, $email, $sha1_password, $activationCode));
    }

    function uyeOnay($registrationCode) {
        $query = "SELECT id FROM pasaj_register where activationCode = '" . $registrationCode . "' and active != 1";
        $result = $this->db->query($query, $registrationCode);

        if ($result->num_rows() == 1) {
            $query = "UPDATE pasaj_register SET active = 1 WHERE activationCode = ?";
            $this->db->query($query, $registrationCode);

            return true;
        } else {
            return false;
        }
    }

     function girisKontrol($username, $password) {
        $sha1_password = sha1($password);
        $query = "SELECT id FROM pasaj_register WHERE username = ? and password = ?";

        $result = $this->db->query($query, array($username, $sha1_password));

        if ($result->num_rows() == 1)
            return $result->row(0)->id;
        else
            return false;
    }



}

In giris controller i use girisKontrol function

<?php

class giris extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->load->model('kayit/kayitmodel');
        $this->load->view('giris/giris');
    }


    public function main_page() {

        extract($_POST);


        $userID = $this->giris->kayitmodel($username, $password);


        if(!userID)
            echo "yok";
        else 
            echo "var"; 
    }

}

?>

but when page is processed it gives error :

Fatal error: Call to a member function kayitmodel() on a non-object in C:\xampp\htdocs\pasaj\application\controllers\giris.php on line 20

why ?

Mert METİN
  • 1,258
  • 6
  • 22
  • 31
  • 2
    possible duplicate of the entire Related section – Gordon Feb 29 '12 at 18:58
  • possible duplicate of [Call to a member function on a non-object](http://stackoverflow.com/questions/54566/call-to-a-member-function-on-a-non-object) – Mike B Feb 29 '12 at 19:14

1 Answers1

2
 $userID = $this->giris->kayitmodel($username, $password);

This is wrong. giris is your controller, it is currently $this. kayitmodel is your model. You then need to call a function on your model.

 $userID = $this->kayitmodel->girisKontrol($username, $password);

Also in your model:

function User_model() {
    parent::Model();
}

should be:

public function __construct() {
    parent::__construct();
}

EDIT: Models need to start with an uppercase letter, with the rest lowercase. Also the file name should be the class name, but all lowercase.
Manual: http://codeigniter.com/user_guide/general/models.html

This should be in a file called kayitmodel.php (note the lowercase 'k').

class Kayitmodel extends CI_Model { // Note the capital 'K'

Your call should be changed to:

$userID = $this->Kayitmodel->girisKontrol($username, $password); // Note the capital 'K'

EDIT2: Your controller should start with an uppercase letter too.
Manual: http://codeigniter.com/user_guide/general/controllers.html

class Giris extends CI_Controller { // Note the capital 'G'

EDIT3: You need to load the model in the constructor of your controller, so all methods inside can use it.

class Giris extends CI_Controller {

    public function __construct() {
        parent::__construct();  // Make sure this is the 1st line in the constructor
        $this->load->model('kayit/kayitmodel');
    }
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Really, there's no need for either `function User_model()` or the manual `parent::__construct()`. This code is very confusing. `extract($_POST);` is also scary and doing nothing. – Wesley Murch Feb 29 '12 at 19:06
  • @Madmartigan: It seems like `function User_model()` was there because he copy&pasted it from another function, and didn't change it. `parent::__construct()` is needed, you need to call the `CI_Model` constructor in your model's constructor. – gen_Eric Feb 29 '12 at 19:07
  • No you don't have to call it manually, the parent constructor always runs, and all the `CI_Model` constructor does is log a debug message that it was loaded. You have to call it if you overload the contstructor, but not if you don't. – Wesley Murch Feb 29 '12 at 19:09
  • there is no change. still says Fatal error: Call to a member function girisKontrol() on a non-object in C:\xampp\htdocs\pasaj\application\controllers\giris.php on line 20 – Mert METİN Feb 29 '12 at 19:11
  • 1
    @MertMETİN: It's because you didn't load the model in the method that calls on it. Load the model in your controller's `__construct()` to make it available to the other methods. – Wesley Murch Feb 29 '12 at 19:12
  • @MertMETİN: According to [the manual](http://codeigniter.com/user_guide/general/models.html), your model needs to start with a uppercase letter. – gen_Eric Feb 29 '12 at 19:12
  • @Madmartigan: Wow, I didn't notice that >. – gen_Eric Feb 29 '12 at 19:17