Welcome Guest, Not a member yet? Register   Sign In
Problem with my custome library
#1

[eluser]opterons[/eluser]
Hello all,
I am a tad bit new to code igniter i understand allot of how ci works but i have got to the part in the manual where you can make your own library which i have but for some reason i can not load database() in my library im sure it is something stupid that i am missing but i thought i would give this forum a try.

Here is the controller code that i am using.

Controller, Login:
Code:
<?php

class Login extends Controller {

    function index()
    {
$this->load->library('accounts');
        $this->load->helper('security');
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            $check = $this->accounts->check_login($username,$password);
if ($check == true) {echo 'hell ya';}
else {echo 'hell na';}
            
    }
    
}
?>
the error is in the library on line 7 which is this line:
Code:
$this->load->database();

Here is the entire code that i have in that library

Library, Accounts:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Accounts {

    function check_login($username,$password)
    {
            $this->load->database();
            $query = $this->db->query('SELECT username, password FROM wz_users');
            $login = false;
        foreach ($query->result_array() as $row) {
            if ($row['username'] == $username){
                if ($row['password'] == $password) {
                    $login = true;
                }
            }
        }
        return ($login);
    }
}

?>

When i try to run my root directory root/login i get this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Accounts::$load

Filename: libraries/Accounts.php

Line Number: 7


Now i am sure im doing something stupid being that i am new to ci but i am stumped maybe someone out there could possibly give me a hand.
#2

[eluser]Ivan A. Zenteno[/eluser]
Do you load the database library, into autoload?

If you not load database load it
#3

[eluser]opterons[/eluser]
I just found that but still no go i changed the code to:

Code:
$autoload['libraries'] = array('database');

If that is what im suppose to do there then im still not getting it to load the database it errors right here in the library:

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

class Accounts {

    function check_login($username,$password)
    {
    echo $username,'<br/>',$password,'<br/>before';
            $this->load->database();
    echo 'after';

Note that i have it echoing the user name and password and before right before the library is called but it errors right on database()
#4

[eluser]Ivan A. Zenteno[/eluser]
but that tells you exactly is wrong?
#5

[eluser]Ivan A. Zenteno[/eluser]
Simple question, Do you wrote user, password and stuff in your config/database.php file?
#6

[eluser]opterons[/eluser]
Of course i had this working properly as a controller

Code:
$this->load->database();
        $this->load->helper('security');
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            $query = $this->db->query('SELECT username, password FROM wz_users');
            $login = false;
        foreach ($query->result_array() as $row) {
            if ($row['username'] == $username){
                if ($row['password'] == $password) {
                    $login = true;
                }
            }
        }
        if ($login == true) {
            echo 'hell ya';
        }
        else {
            exit('hell na');
        }

That code works by it self as a controller and checks weather the user is there then checks weather password for the user is correct. the problem is when i use $this->load its not just database its anything with $this->load hence the error:

Message: Undefined property: Accounts::$load

like if i try to load the session library it will give me the same error :/
#7

[eluser]ejangi[/eluser]
Libraries do not have access to the CI_Base class, so they essentially don't have access to any other parts of the code igniter framework, but if you want them to, you need to get the Controller instance like so:
Code:
class MY_Class {
    
    var $CI = null;
    
    function MY_Class()
    {
        $this->CI =& get_instance();
    }
    
    function my_other_function()
    {
        $this->CI->db->get('my_table');
        ...
    }
    
}

So, yeah... All the magic happens in $this->CI =& get_instance();
You can also do this in helpers as well, except that you don't use $this, just $CI instead.

Hope this helps. ;-)




Theme © iAndrew 2016 - Forum software by © MyBB