简体   繁体   English

Codeigniter全局数组声明

[英]Codeigniter Global Array Declaration

I have a sequence of number like follows 我有一个数字序列如下

1 -> 25, 2 -> 60, 3 -> 80, 4 -> 100 and so on 1-> 25、2-> 60、3-> 80、4-> 100等

which means that if input is 1 output will be 25 and so on...I need to store it in global array.I would like to use it in multiple pages also.In codeigniter where i can declare a global array and store all these? 这意味着如果输入为1,输出将为25,依此类推...我需要将其存储在全局数组中。我也想在多个页面中使用它。在codeigniter中,我可以声明一个全局数组并存储所有这些?

I am trying like as follows in constants.php 我正在constants.php中尝试如下

$CONFIDENCEVALUE = array(); $CONFIDENCEVALUE[] = array('1'=>25,'2'=>'60','3'=>80,'4'=>100);

If it is correct how can access these array value in required pages.Help me please.I am not an expert with codeignitor. 如果正确,如何在所需页面中访问这些数组值。请帮助我。我不是codeignitor的专家。

If I were you I'd look at adding a custom config file (see https://www.codeigniter.com/user_guide/libraries/config.html ). 如果您是我,那么我会考虑添加一个自定义配置文件(请参阅https://www.codeigniter.com/user_guide/libraries/config.html )。

So in eg. 所以在。 application/config/confidencevalue.php add the following application/config/confidencevalue.php添加以下内容

$CONFIDENCEVALUE = array('1'=>25,'2'=>'60','3'=>80,'4'=>100);
$config['confidencevalue'] = $CONFIDENCEVALUE;

Add the config file to your application/config/autoload.php and you'll then be able to access your array through the config class using $this->config->item('1', 'confidencevalue'); 将配置文件添加到application/config/autoload.php ,然后您可以使用$this->config->item('1', 'confidencevalue');通过config类访问数组$this->config->item('1', 'confidencevalue'); (replacing the 1 for the value you're looking for). (将1替换为您要查找的值)。

One way of doing this is by adding a function to a helper file that you make available globally. 一种实现方法是将功能添加到全局可用的帮助文件中。

I have a helper file application/helpers/main_helper.php in which I load a number of generic, common functions which are used throughout my application. 我有一个帮助程序文件application / helpers / main_helper.php,其中加载了许多通用的,通用的功能,这些功能在我的整个应用程序中都使用。

If you add the following function to the main_helper file: 如果将以下函数添加到main_helper文件中:

/*
|--------------------------------------------------------------------------
| Function to retrieve Static Variables used Globally
|--------------------------------------------------------------------------
*/
function get_var($var = 'CONFIDENCEVALUE', $KEY = NULL) {
    $r = false;
    switch ($var) {
        case 'CONFIDENCEVALUE':
            $r = array('1'=>25,'2'=>'60','3'=>80,'4'=>100);
            if($KEY !== NULL) $r = $r[$KEY];
            break;
    }
    return $r;
}

This file is auto-loaded by editing the file application/config/autoload.php and editing the line: 通过编辑文件application / config / autoload.php并编辑以下行来自动加载此文件:

$autoload['helper'] = array('main_helper');

Whenever this array (or a value from the array) is needed, call the function instead. 每当需要此数组(或数组中的值)时,请调用该函数。 eg.: 例如。:

$CONFIDENCE = get_var('CONFIDENCEVALUE', 2); 

If you include the $KEY when calling get_var(), then only the value is returned, otherwise the whole array is returned. 如果在调用get_var()时包含$ KEY,则仅返回值,否则返回整个数组。

To make additional variables available, just add them to the switch and call them as needed. 要使其他变量可用,只需将它们添加到开关并根据需要调用它们。 Feedback welcome :). 欢迎反馈:)。

Store the array in a session variable: 将数组存储在会话变量中:

$this->session->set_userdata('cvarray', $CONFIDENCEVALUE);

To access the array later: 以后要访问阵列:

$this->session->userdata('cvarray');

CodeIgniter Session Class CodeIgniter 会话类

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

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