Welcome Guest, Not a member yet? Register   Sign In
How to initilize an array data while app started?
#1

[eluser]liren[/eluser]
Hello!

I want to init an array data in memory when my webapp start.How to do it? If the autoloader could do it,please show my an example,thanks@!
#2

[eluser]toopay[/eluser]
write a config file, for example 'myarray.php' then put into 'application/config', then you can write some array on it like...
Code:
<?php defined('BASEPATH') or die('No direct script access allowed');
$config['some_array']    = array(
    'var1' => 'foo',
    'var2' => 'bar',
);
load it in autoloader, on config section like...
Code:
...
$autoload['config'] = array('myarray');
...
Now, try to fetch that array in any controller using...
Code:
var_dump($this->config->item('some_array'));
#3

[eluser]liren[/eluser]
[quote author="toopay" date="1302260635"]write a config file, for example 'myarray.php' then put into 'application/config', then you can write some array on it like...
Code:
<?php defined('BASEPATH') or die('No direct script access allowed');
$config['some_array']    = array(
    'var1' => 'foo',
    'var2' => 'bar',
);
load it in autoloader, on config section like...
Code:
...
$autoload['config'] = array('myarray');
...
Now, try to fetch that array in any controller using...
Code:
var_dump($this->config->item('some_array'));
[/quote]

Thanks toopay:

no,my config array is read from mysql database. when my webapp start,I want to app read database to memory,then I could using the config array anywhere in my webapp. How to do it?
#4

[eluser]toopay[/eluser]
If you are about loading some data from database (not static resources like aboves), then want it available for any controller, you have several choice.
1. Make a "Base Controller" to load all necessary resource. Then other controller can use that resource by extending your "Base Controller".
2. As i said on your other thread, you can do that by configure some ‘pre_controller’ hook. Write a "Init" class which contain function to load your resource before all controller being called.
3. If you are want more clean and structural app, write some modules to handle that.
#5

[eluser]liren[/eluser]
[quote author="toopay" date="1302263101"]If you are about loading some data from database (not static resources like aboves), then want it available for any controller, you have several choice.
1. Make a "Base Controller" to load all necessary resource. Then other controller can use that resource by extending your "Base Controller".
2. As i said on your other thread, you can do that by configure some ‘pre_controller’ hook. Write a "Init" class which contain function to load your resource before all controller being called.
3. If you are want more clean and structural app, write some modules to handle that.[/quote]

If you are about loading some data from database (not static resources like aboves), then want it available for any controller, you have several choice.
1. Make a “Base Controller” to load all necessary resource. Then other controller can use that resource by extending your “Base Controller”.
2. As i said on your other thread, you can do that by configure some ‘pre_controller’ hook. Write a “Init” class which contain function to load your resource before all controller being called.
3. If you are want more clean and structural app, write some modules to handle that.


The problem is: how to write a modules which build an array from database.then I could read and edit this array in my webapp? I open some autoload class,but when I restart apache server,the log don't output any initialize class log,like Log.php:
Code:
class CI_Session {

    var $sess_encrypt_cookie        = FALSE;


    /**
     * Session Constructor
     *
     * The constructor runs the session routines automatically
     * whenever the class is instantiated.
     */
    public function __construct($params = array())
    {
    log_message('debug', "Session Class Initialized"); //why don't log to logfile?
#6

[eluser]toopay[/eluser]
Why i fell that in your case, 'pre_controller' hook is more than enough? You use module for something more sophisticated than that, like authentification stuff, templating stuff, etc.
#7

[eluser]liren[/eluser]
[quote author="toopay" date="1302269493"]Why i fell that in your case, 'pre_controller' hook is more than enough? You use module for something more sophisticated than that, like authentification stuff, templating stuff, etc.[/quote]

Do you mean that this array will be init when every user send a request to server? But I want to this array will be init only once when server start.
#8

[eluser]toopay[/eluser]
There are no such things! Except you are using memcache, CI will send request to your database, everytime you load/init your resources.
#9

[eluser]InsiteFX[/eluser]
Code:
$data = array();

foreach ($your_array() as $key => $value)
{
    $data[$key] = $value;
}

return $data;

InsiteFX
#10

[eluser]liren[/eluser]
[quote author="toopay" date="1302270470"]There are no such things! Except you are using memcache, CI will send request to your database, everytime you load/init your resources.[/quote]

Thank you toopay!I search many article in Google,find that you are right.it's not possible to do it in PHP,So sad




Theme © iAndrew 2016 - Forum software by © MyBB