Welcome Guest, Not a member yet? Register   Sign In
I want to define variable in CodeIgnitor so that it can be accessed in application
#1

[eluser]Musaddiq Khan[/eluser]
Actually I want to declare an array of constants.i.e
define('USER_ADMIN',1);
define('USER_MEMBER',2);

Now I want to store these constants in array like.

$user_type[USER_ADMIN]='Admin';
$user_type[USER_MEMBER]='Member';

It's requirements so that this array of constants should be accessible in application directory, i.e in controllers, views, models etc.

Thanks
#2

[eluser]Aken[/eluser]
You can define your own constants in application/config/constants.php.

You can define your own config items available application-wide using the Config library. http://ellislab.com/codeigniter/user-gui...onfig.html
#3

[eluser]Musaddiq Khan[/eluser]
I have tried to put this array of constants in config/constants.php but it generates an error message on accessing array.
The second option is much better.
Is it possible to use this kind of array of constants without including any file same as we use constants. e.g here we need to load config item
#4

[eluser]Aken[/eluser]
You can autoload custom config files.
#5

[eluser]Musaddiq Khan[/eluser]
thanks
#6

[eluser]Musaddiq Khan[/eluser]
I have created a file in application/config/user_constants.php and this file is auto loaded.
Code:
$config = array(
                'user_type' => array(CONST_ADMIN=> 1,CONST_MEMBER=> 2),
  'commission' => array(COMMISSION_HALF=>1,COMMISSION_FULL=>2)                        
               );
I get the commission type by coding below.
Code:
$commission = $this->config->item('commission');
It works fine.

Is it possible to us $commission without manually loading this config item.

Thanks
#7

[eluser]InsiteFX[/eluser]
You can also create your own helper file to return what you need.




Theme © iAndrew 2016 - Forum software by © MyBB