views:

5222

answers:

5

Hi, 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

Can I do it?? How?? Thanks!!

+20  A: 

A CodeIgniter helper is a PHP file with multiple methods. It is not a class

Create a file and put the following code into it.

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('test_method'))
{
    function test_method($var = '')
    {
     return $var;
    } 
}

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)

$this->load->helper('new_helper');

echo test_method('Hello World');

If you use this helper in a lot of locations you can have it load automatically by adding it to the autoload configuration file.

$autoload['helper'] = array('new_helper');

-Mathew

The Pixel Developer
A: 

why you dont create a logic in the controller and supply different array types based on that into the view.

maybe you can create different views for different type of array too

r4ccoon
+2  A: 

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:

{blog_entries}
<h5>{title}</h5>
<p>{body}</p>
{/blog_entries}

instead of

<?php foreach ($blog_entries as $blog_entry): ?>
<h5><?php echo $blog_entry['title']; ?></h5>
<p><?php echo $blog_entry['body']; ?></p>
<?php endforeach; ?>

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.

fandelost
+8  A: 

hi. got back here after awhile huh?

i just wanna write some codes that allow to use CI instance inside the helper

  function yourHelperFunction(){
        $ci=& get_instance();
        $ci->load->database(); 

        $sql = "select * from table"; 
        $query = $ci->db->query($sql);
        $row = $query->result();
   }
r4ccoon
thank you for showing how to use the CI instance.
NTulip
no worries mate
r4ccoon
A: 

checking to see if the *function_exists* is not quite required I guess, because even if it does the script is going to show an error when called!

thanks!