0

I have a helper file, it has a variable that I want to pass across to a view, but it comes across as empty, so I am a bit unsure if I have the right code or I have overwritten it later on _ though I am sure I have not!

anyway say the variable in ther helper file is an array that contains a list of data, and I use:

$this->load->helper('helperfile_helper'); //contains the variable 'productList'
$data['productList'] = $productList;
$this->load->view('page', $data);

I would expect that the helper file works like an 'include' with the defined variables available once the helper has been called, is this the casee or have I missed something??

2
  • Can we see the code for helperfile_helper? Commented Dec 17, 2013 at 17:18
  • thats just an example, but basically it held a list of predefined variables such as $productList = array(blah=>blah); Commented Dec 18, 2013 at 14:54

1 Answer 1

4

Helpers allow you to use function in your controller, have a look here: http://ellislab.com/codeigniter%20/user-guide/general/helpers.html

So you must create a function in your helper file that will return a value.

For example in your helper:

if( ! function_exists('random_number'))
{
    function random_number()
    {
return 4;
    }
}

and in your controller you can use it:

$this->load->helper('helperfile_helper'); //contains the variable 'productList'
$data['random_number'] = random_number();
$this->load->view('page', $data);

So $data['random_number'] will contain 4

Sign up to request clarification or add additional context in comments.

2 Comments

You sir, are a diamond, if I have enough rep I will certainly upvote you for it!
+1 "So you must create a function in your helper file that will return a value." So means helper can't access my controller variables and vice verse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.