Welcome Guest, Not a member yet? Register   Sign In
GLOBAL variable
#11

[eluser]Derek Allard[/eluser]
Dinesh, take a moment to read the sessions page. Seriously, its easy. I'm not sure I see why it "wont be a good idea".

That said, if you wanted to use global vars, CI does nothing to stop this, and you could just use a simple include() or require() if you wanted.
#12

[eluser]mistersmith[/eluser]
Hi All,

Getting back to the Global variables thing..

What would be the best practice for this scenario:

Two CodeIgniter applications running side by side (in separate folders), both sharing the same System folder, and both connecting to the same database. One application is the CMS for the other. Presently, each application has it's own Config folder, but I would like for them both to share certain settings (db config etc) from a central file on the root of the server. For now, I am using "require" to include this file in four places - for each application's config and database config files. I'm certain that there is a better way.

How do I load this central config file so that both applications can use it's settings?

Or, more specifically, how do I make these settings available during the CodeIgniter startup sequence (ie before config.php gets loaded)?

I don't want to throw them in $GLOBAL, I would like to attach them to the CodeIgniter instance namespace.

Any guidance would be much appreciated.

Cheers,
Shaun
#13

[eluser]Colin Williams[/eluser]
in one of the apps config.php files just include the other

Code:
include '/path/to/core/config.php';
#14

[eluser]mistersmith[/eluser]
Thank you for the reply Colin. While I'm sure that would work, I'm looking for a slightly different solution.

I would like to have a single (fairly small) config file that sits outside of both the application folders, and I need it's settings to be available to both app's config files as well as a few other places in both applications.

With the Zend Framework, there is a Registry component for doing this kind of thing. I'm just wondering what the CodeIgniter equivalent is.

Many thanks,
#15

[eluser]mistersmith[/eluser]
What I did in the end:

I put a file on the root of the server that declares a single associative array with my custom settings in it.

Added a "require" to the top of my config/config.php file, and stored the associative array from that file in the standard CodeIgniter config array:

require('config'.EXT);
$config['custom_settings'] = $custom_settings;

Then, later in my app, inside Models or Controllers, I can access those settings with:

$custom_settings = $this->config->item('custom_settings');
$some_val = $custom_settings['some_key'];

Inside Libraries, however, I needed to do it this way:

$ci =& get_instance();
$custom_settings = $ci->config->item('custom_settings');
$some_val = $custom_settings['some_key'];

It'll have to do for now I suppose Smile
#16

[eluser]Unknown[/eluser]
Hi! i would like to ask if there is a workaround in CI in using global variable that we can call in a function and forgets the global var value after the page execution.
code below

<code>
$g_bar = '';

function do_x()
{
global $g_bar;

for( $i=0, $a=func_num_args(); $i<$a; $i++ ){
$args = func_get_arg($i);
$g_bar .= 'nice '. $args. '<br />';
}
}

....

do_x( 'a', 'b', 'c');
....
echo $g_bar;

</code>


i know i can compute the value in the controller. but this one i put in view.
your enlightenment is very much appreciated.
#17

[eluser]jtrainaldi[/eluser]
How would you deal with a variable that you plan on using multiple times within your site. For example I have an array of states ($state_list) that will populate a drop down form with all of the states of the US.

Rather than copying and pasting the array on each form what is the preferred method of assigning that variable "globally" and re-using it throughout the site.

Thanks in advance.
#18

[eluser]Mike Chojnacki[/eluser]
[quote author="jtrainaldi" date="1310523796"]How would you deal with a variable that you plan on using multiple times within your site. For example I have an array of states ($state_list) that will populate a drop down form with all of the states of the US.

Rather than copying and pasting the array on each form what is the preferred method of assigning that variable "globally" and re-using it throughout the site.

Thanks in advance.[/quote]

Create a helper and give it a function like:

print_state_list()
{
// your array
$state_list = array();
// return the array
return $state_list();
}

Then add the helper to autoload in the autoload file in the config folder and you can just call &lt;?=print_state_list()?&gt; in any view to access the state list.




Theme © iAndrew 2016 - Forum software by © MyBB