I need to loop lot of arrays in different ways and display it in a page. The arrays are generated by a module class. I know that its better not to include functions on 'views' and I want to know where to insert the functions file.
I know I can 'extend' the helpers, but I don't want to extend a helper. I want to kind of create a helper with my loop functions.. Lets call it loops_helper.php
Some code that allows you to use CI instance inside the helper:
Well for me only works adding the text
"_helper"
after in the php file like:And to load automatically the helper in the folder aplication -> file autoload.php add in the array helper's the name without "_helper" like:
And with that I can use all the helper's functions
A CodeIgniter helper is a PHP file with multiple functions. It is not a class
Create a file and put the following code into it.
Save this to application/helpers/ . We shall call it "new_helper.php"
The first line exists to make sure the file cannot be included and ran from outside the CodeIgniter scope. Everything after this is self explanatory.
Using the Helper
This can be in your controller, model or view (not preferable)
If you use this helper in a lot of locations you can have it load automatically by adding it to the autoload configuration file i.e.
<your-web-app>\application\config\autoload.php
.-Mathew
To retrieve an item from your config file, use the following function:
$this->config->item('item name');
Where item name is the $config array index you want to retrieve. For example, to fetch your language choice you'll do this:$lang = $this->config->item('language');
The function returns FALSE (boolean) if the item you are trying to fetch does not exist.If you are using the second parameter of the $this->config->load function in order to assign your config items to a specific index you can retrieve it by specifying the index name in the second parameter of the $this->config->item() function. Example:
// Loads a config file named blog_settings.php and assigns it to an index named "blog_settings"
// Retrieve a config item named site_name contained within the blog_settings array
// An alternate way to specify the same item:
$site_name = $blog_config['site_name'];
Create a file with the name of your helper in /application/helpers and add it to the autoload config file/load it manually.
E.g. place a file called user_helper.php in /application/helpers with this content:
Now you can either load the helper via
$this->load->helper(‘user’);
or add it to application/config/autoload.php config.To create a new helper you can follow the instructions from The Pixel Developer, but my advice is not to create a helper just for the logic required by a particular part of a particular application. Instead, use that logic in the controller to set the arrays to their final intended values. Once you got that, you pass them to the view using the Template Parser Class and (hopefully) you can keep the view clean from anything that looks like PHP using simple variables or variable tag pairs instead of echos and foreachs. i.e:
instead of
Another benefit from this approach is that you don't have to worry about adding the CI instance as you would if you use custom helpers to do all the work.