2

I have a sequence of number like follows

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

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?

I am trying like as follows in 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.

3 Answers 3

8

If I were you I'd look at adding a custom config file (see https://www.codeigniter.com/user_guide/libraries/config.html).

So in eg. application/config/confidencevalue.php add the following

$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'); (replacing the 1 for the value you're looking for).

Sign up to request clarification or add additional context in comments.

1 Comment

Now how do I add another value to this array dynamically? and delete one with key 2?
0

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

Comments

0

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.

If you add the following function to the main_helper file:

/*
|--------------------------------------------------------------------------
| 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:

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

To make additional variables available, just add them to the switch and call them as needed. Feedback welcome :).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.