Welcome Guest, Not a member yet? Register   Sign In
Global $data array?
#1

[eluser]Zero-10[/eluser]
I have a $data array that I would like to load on all pages of my site so I don't have to have the same code over and over again Wink

I've been trying to figure this one out since 3 and it's 5:41 now @__@ (i should sleep)

There are also some things like load model with these settings etc that I'd like to do and I couldn't find any info on how to do it with autoload. At the moment I have it all loaded inside a model as function __construct() { which doesn't seem to do very much with the data array... It'll load the models but not the array info Sad

Thank you in advance for your help!
#2

[eluser]skunkbad[/eluser]
There are a few different ways you could make an array that is global. If you need the array to be present only in views, you could use $this->load-vars() in a MY_Controller. If you want to have the array available in controllers, libraries, models, etc., then you could put the array in a pre-init hook. You could also try loading the array into a constant in your application/config/constants.php file. There are some other ways too, but I think these three were the first in my head.
#3

[eluser]Zero-10[/eluser]
Thanks for the response! Unfortunately I couldn't understand how to setup constants from the documentations and the other threads in the forums. I do need it for different controllers, models, views and possibly libraries in the future so I'm sure the constants approach would be favorable.

I would need something like this:
Code:
$this->load->model('articles/get_article','',TRUE);

if ($this->phpbb_library->isLoggedIn() === TRUE)
        {
            $data = array(
                                'id'            => $this->phpbb_library->getUserInfo('user_id'),
                                'username'      => $this->phpbb_library->getUserInfo('username') etc...

Could you please provide an example on how I could implement it within config/constants.php please? It looks like gibberish and it seems like everyone secretively understands how it works but me :/
#4

[eluser]skunkbad[/eluser]
I've seen where people just added to the application/config/constants.php, but I don't like use that method because there are some default values in there, and to me this stuff ought to be in the system directory. Anyways, to add a constant, you just create another entry:

Code:
define('SOMETHING', array(
  'value',
  'value-two',
  'etc'
));

Then, in the place where you want to use the array, you access it like this:

Code:
var_dump( SOMETHING );

As far as I know the constants defined in application/config/constants.php should be loaded early enough that you could use them anywhere, but if not, then make a pre-init hook and define the array there. I do this in Community Cart, in case you want to see an example. The hook is located at application/hooks/my-site-definitions-hook.php. Community Cart link in my signature.
#5

[eluser]Zero-10[/eluser]
Thanks! This makes sense to me =)
#6

[eluser]jordantkj[/eluser]
There was a post on this thread that showed an array being declared as a constant. I tried this, but it isn't working for me. I get the following warning:

<b>Warning: Constants may only evaluate to scalar values</b>

Can anyone verify that it is possible to define an array as a constant?

[quote author="skunkbad" date="1309739864"]...Anyways, to add a constant, you just create another entry:

Code:
define('SOMETHING', array(
  'value',
  'value-two',
  'etc'
));
[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB