CodeIgniter Forums

Full Version: declaring class level variables in codeigniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Syed Wasti[/eluser]
Hi,

I am new to CI and what I want to do is to have a class level variable (which e.g is an array). But it seems like CI, despite all high bragging, doesn't support this feature. Nothing has been mentioned in the user guide about it. There is a heading called private functions and variables but the text has been seemingly kept silent regarding variables.

I want to have something like -->
Code:
class OrderStats extends CI_Controller {

protected $arr_CoreCountry = ('0'=>'uk', '1'=>'us');

public function __construct()
       {
      
            parent::__construct();
            // Your own constructor code
        
       }

       public function index()
    {
        
        $this->load->model('orders', '', TRUE);
        //$data['result'] = $this->Testmodel->get_entries();
        $data['result'] = $this->Testmodel->get_reports();
        $this->load->view('test', $data);
                        
    }
public function getOrderStats()
    {      
                $this->load->model('Orderstatsmodel', '', TRUE);
                
                //$data['result'] = $this->Testmodel->get_entries();
                foreach ($arr_CoreCountry as $key => $value)
                {
                    $data['result'] = $this->Orderstatsmodel->get_orderStats($key);
                }
//                $data['result'] = $this->Orderstatsmodel->get_orderStats(0);
                $this->load->view('orderstats', $data);
                        
    }

I keep getting an error messag that $arr_CoreCountry is an undefined variable. Where do I define it? Am I declaring it at a wrong place?

Expect a quick response as half of my day has been wasted just because of this s*** from codeigniter.

El Forum

[eluser]Seb[/eluser]
Well this has nothing to do with codeigniter, but it is just basic PHP coding: http://www.php.net/manual/en/language.oop5.basic.php

You should type :

Code:
foreach ($this->arr_CoreCountry as $key => $value)

instead of

Code:
foreach ($arr_CoreCountry as $key => $value)

El Forum

[eluser]Syed Wasti[/eluser]
Well yes, it really is a question of basics.

When you write $this, it generates an error message that $this can't be used when not in an object's context.

El Forum

[eluser]Armchair Samurai[/eluser]
Seb's absolutely correct - this has nothing to do with CI and everything to do with your code.

Maybe you should brush up on your PHP OOP.