Welcome Guest, Not a member yet? Register   Sign In
Array stored in helper, access from view?
#1

[eluser]batfastad[/eluser]
Hi everyone

There is a data file on the server that is shared between many different projects on the same server. I am working on a project in CodeIgniter, all the other projects are bespoke/procedural.

This data file contains a multidimensional array of data and several procedural functions for processing that data in different ways. The format of that data file has to stay the same so it can be used by other projects. This is not stored in a database because it is needed so frequently by many different projects and this reduces the number of database queries dramatically.

Right now I need access to that array and a couple of the functions within my CodeIgniter project. Ideally I'd like to access this array and related functions from my view (although eventually setting it up as a model could make more sense) because the data is used to process variables just before output to the page.

I've created a temporary helper which then imports the actual data file, the path is stored in config...
Code:
$data = array();
require_once($this->config->item('coname_shared_root').'global_data.php');

Then I'm loading the helper in my view (I know this is really not the way things should be done)...
Code:
$this->load->helper('globaldata');

How can I access that array in my view to do lookups?

Or if not a helper, how can I wrap it up into a library without touching the original data file structure/format?

Cheers, B
#2

[eluser]LuckyFella73[/eluser]
Your helper has to return the data you need.

Code:
... more code here
return $data;

If you show the helper code we could be more precise about that.

See more about CI helpers here:
http://ellislab.com/codeigniter/user-gui...lpers.html
#3

[eluser]Massaki[/eluser]
You can do the procedural way to include the 'helper'.
Put this inside the method:
include 'application/helper/globaldata.php';
#4

[eluser]batfastad[/eluser]
Ok perfect. I've gone the procedural way for now, as Massaki mentioned. Thanks!
It's at least working and that gives me some time to think of a better structure.

Cheers, B
#5

[eluser]CroNiX[/eluser]
For multi sites on my server, I usually do:

/home/CI_SYSTEM/2_1 (common system folder all apps to share)
/home/common/ (files common to all sites, or some sites)
/home/site1/public_html (uses CI_SYSTEM/2_1 and also includes data from /home/common)
/home/site2/public_html (uses CI_SYSTEM/2_1 and also includes data from /home/common)

The sites just include data from /home/common as it needs them, if it needs them.

Then when I want to test the sites on whatever CI version that comes out next, I just make a new subdir in /home/CI_SYSTEM for that version number and switch each site one by one to test on that version (in index.php).




Theme © iAndrew 2016 - Forum software by © MyBB