Welcome Guest, Not a member yet? Register   Sign In
array in controller not recognized in rest of class' functions?
#8

[eluser]Pascal Kriete[/eluser]
Ok, I think the first thing we should clear up is the scope problem. Every variable is set in what is called a scope - most of the time it just means that it's inside curly braces.
Example:
Code:
class Something {
    $somevar = 'something';    //This is available in the entire class using $this->somevar ($this refers to the current class)

    function test() {
        $testvar = 'somethingelse';    //This is available in the test function, but not outside it.
    }

    function print() {
        $a = 1;
        if ($a == 1) {
            echo $a;    //No problem
        }

        echo $this->somevar;    //Prints 'something'
        echo $testvar;       //Not defined in this scope
    }
}
I hope that helps a bit.


So now we'll get to your class:
Code:
class Athlete extends Controller {

    /* Add this to make $rules a class variable */
    $rules = array();

    function Athlete()
    {
        // parent::Controller();
        $this->load->helper('url');
        $this->load->helper( array('form','array','scott') );
        $this->load->library('validation');
        $this->load->model('Scott_model');

        // VALIDATION

        /* Change all these to $this->rules... */
        $this->rules['AthleteTitle'] = '';
        $this->$rules['AthleteNameFirst'] = 'trim|required|max_length[50]';
        $this->$rules['AthleteNameMiddle'] = 'trim|max_length[50]';
        $this->$rules['AthleteNameLast'] = 'trim|required|max_length[50]';
        $this->$rules['AthleteGender'] = '';
        $this->$rules['AthleteHOFMember'] = '';
        $this->$rules['AthleteHOFInductionDate'] = 'trim|callback__hof_check|numeric';
        $this->$rules['AthletePhotoUCLA'] = '';
        $this->$rules['AthletePhotoPostUCLA'] = '';
        $this->$rules['AthleteVideoUCLA'] = '';
        $this->$rules['AthleteVideoPostUCLA'] = '';
        $this->$rules['AthleteStoryUCLA'] = 'trim|htmlspecialchars';
        $this->$rules['AthleteStoryPostUCLA']  = 'trim|htmlspecialchars';
        
        $this->validation->set_rules($this->$rules);
    }
    ...
    // Also change any other occurences of $rules to $this->rules...
}
OOP takes a while to get your head round, and PHP isn't really a great one to learn it with, but you'll get the hang of it.

I hope I got all that right (it's almost 3am), and I hope it helps Smile.

EDIT: Forgot this part: There is a CodeIgniter object, that most of the time you just call using $this->whatever (load, etc). That object is only defined in the views, models, controllers. To get it in your own classes you do $CI =& get_instance(). The & means that you're getting a reference (a pointer at the object) instead of copying it. And now you can use $CI as you would $this.


Messages In This Thread
array in controller not recognized in rest of class' functions? - by El Forum - 01-23-2008, 07:53 PM



Theme © iAndrew 2016 - Forum software by © MyBB