• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
declaring class level variables in codeigniter

[eluser]Syed Wasti[/eluser]

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 -->
class OrderStats extends CI_Controller {

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

public function __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.

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 :

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

instead of

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

[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.

[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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.