简体   繁体   English

Codeigniter-从帮助文件访问变量-不是类

[英]Codeigniter - Accessing variables from a helper file - not a class

I have a helper file, it has a variable that I want to pass across to a view, but it comes across as empty, so I am a bit unsure if I have the right code or I have overwritten it later on _ though I am sure I have not! 我有一个帮助程序文件,它具有要传递给视图的变量,但它为空,因此我不确定我是否具有正确的代码,或者稍后在_上覆盖了它,尽管我确定我没有!

anyway say the variable in ther helper file is an array that contains a list of data, and I use: 无论如何说帮助程序文件中的变量是一个包含数据列表的数组,我使用:

$this->load->helper('helperfile_helper'); //contains the variable 'productList'
$data['productList'] = $productList;
$this->load->view('page', $data);

I would expect that the helper file works like an 'include' with the defined variables available once the helper has been called, is this the casee or have I missed something?? 我希望一旦调用了帮助程序,帮助程序文件就可以像“包含”一样使用定义的变量,这是casee还是我错过了什么?

Helpers allow you to use function in your controller, have a look here: http://ellislab.com/codeigniter%20/user-guide/general/helpers.html Helpers允许您在控制器中使用function ,请在此处查看: http : //ellislab.com/codeigniter%20/user-guide/general/helpers.html

So you must create a function in your helper file that will return a value. 因此,您必须在助手文件中创建一个函数,该函数将返回一个值。

For example in your helper: 例如在您的助手中:

if( ! function_exists('random_number'))
{
    function random_number()
    {
return 4;
    }
}

and in your controller you can use it: 在您的控制器中,您可以使用它:

$this->load->helper('helperfile_helper'); //contains the variable 'productList'
$data['random_number'] = random_number();
$this->load->view('page', $data);

So $data['random_number'] will contain 4 因此$data['random_number']将包含4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM