在 codeigniter 中声明类级别变量

发布于 2024-11-24 20:28:50 字数 1322 浏览 4 评论 0原文

我是 CI 新手,我想做的是拥有一个类级别变量(例如是一个数组)。但尽管 CI 吹嘘得很厉害,但它似乎并不支持这个功能。用户指南中没有提及任何相关内容。有一个标题称为私有函数和变量,但文本似乎对变量保持沉默。

我想要这样的东西:

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);

}

记住,当我在这篇文章中声明 $arr_CoreCountry 变量时,我不断地看到语法错误消息。 当我将它放置在任何函数内部的某个位置时,当然,它会超出范围,并且我不断收到一条错误消息,指出 $arr_CoreCountry 是一个未定义的变量。 那么问题是我在哪里定义它?

期待快速回复,因为仅仅因为 codeigniter 的这个垃圾,我就浪费了一半的时间。

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()
   {

        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);

}

Remember, when I declare $arr_CoreCountry variable at the place as it is in this post, I constantly see a syntax error message.
When I place it some where inside any function then of course, it gets out of scope and I keep getting an error messag that $arr_CoreCountry is an undefined variable.
So the question is where do I define it?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

菩提树下叶撕阳。 2024-12-01 20:28:50

这应该有效:

类 OrderStats 扩展 CI_Controller {

 protected $arr_CoreCountry = array('0'=>'英国', '1'=>'美国'); 

    公共函数 getOrderStats()
    {       
        $this->load->model('Orderstatsmodel', '', TRUE);

        //$data['result'] = $this->Testmodel->get_entries();
        foreach ($this->arr_CoreCountry as $key => $value)
        // ETC

}

您在原始代码中省略了 $this->

编辑
这是我的测试代码〜

class Testing extends CI_Controller {

    protected $foo = array('test'=>'foo', 'bar'=>'baz');

    function index() {
        foreach($this->foo as $k => $v) {
            echo $k . ' = ' . $v . '<br />';
        }
    }
}

// outputs:
test = foo
bar = baz

也许您可以发布您的语法错误,因为它们似乎在您的原始帖子中丢失了。

This should work:

class OrderStats extends CI_Controller {

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

    public function getOrderStats()
    {       
        $this->load->model('Orderstatsmodel', '', TRUE);

        //$data['result'] = $this->Testmodel->get_entries();
        foreach ($this->arr_CoreCountry as $key => $value)
        // etc

}

you are omitting the $this-> in your original code.

Edit
Here was my test code ~

class Testing extends CI_Controller {

    protected $foo = array('test'=>'foo', 'bar'=>'baz');

    function index() {
        foreach($this->foo as $k => $v) {
            echo $k . ' = ' . $v . '<br />';
        }
    }
}

// outputs:
test = foo
bar = baz

perhaps you can post your syntax errors as they appear to be missing from your original post.

晒暮凉 2024-12-01 20:28:50

您有语法数组声明错误。请尝试像这样声明数组:

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

请查看此站点以获取数组手册:http: //php.net/manual/en/language.types.array.php

You have a syntax array declaration error. Please try to declare array like this:

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

Please check out this site for array manual: http://php.net/manual/en/language.types.array.php

满栀 2024-12-01 20:28:50

我自己解决了这个问题。

我更改了两件事

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

更改为

var $arr_CoreCountry = array(0=>'se', 1=>'fi',2=>'de'); 

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

被更改为

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

我缺少 $this 但当我把它放在那里时,它仍然无法工作。当我将 protected 更改为 var 时,它起作用了。

感谢大家的意见...

I solved the problem myself.

There are two things which I changed

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

was changed to

var $arr_CoreCountry = array(0=>'se', 1=>'fi',2=>'de'); 

and

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

was changed to

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

I was missing $this but when I put it there, it was still not working. When I changed protected to var, it worked.

Thanks everyone for your input...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文