Welcome Guest, Not a member yet? Register   Sign In
Correct way to use homemade functions
#1

[eluser]tinawina[/eluser]
This has to be easy! But new to CI and OOP and I'm just not able to get this simple thing to work.

As a first step, I just want to display a page that gets a variable from the data array from a homemade function that I have placed in a homemade helper file. I understand from the User Guide that I can create my own helper files and either place them in the system "helpers" folder or place them in a folder that I create and label "helpers" in my "application" folder. CI will look in the applications/helpers folder first, and if not found there, it will check the system/helpers folder. All of the functions contained in the file can be used throughout my application -- global scope. As long as I label my files so that they end with "_helper.php" I should be good to go.

Ok. I've created a "helpers" folder in my application folder, and I made a helper file called "my_helper.php". Here's the contents of that file:

application/helpers/my_helper.php
Code:
function hello() {
        $data['message'] = 'Hi there!';
    }

Here's my controller file:

application/controllers/test.php
Code:
class Test extends Controller {
    function Test()
    {
        parent::Controller();
        $this->load->helper('my'); // according to the User Guide, I don't have to tell CI where to find this file, or show the "_helper.php" part.
    }

    function index()
    {
        $data['testing'] = "Testing this out.";
        hello();
        $this->load->view('test', $data);
    }
}

And here's my view file:

application/views/test.php
Code:
<html>
<head>
<title>test</title>
</head>
<body>
<p>&lt;?php echo $testing; ?&gt;</p>
<p>&lt;?php echo $message; ?&gt;</p>
&lt;/body&gt;
&lt;/html&gt;

When I peek at this in my browser, I see:

Testing this out.

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: message

Filename: views/test.php

Line Number: 10

So it's showing me $data['testing'] but not $data['message'] which is tucked away in the my_helper.php file but should be available to the entire application. I think.

Really feeling like a neeeewbiiiiiie.... Appreciate any help offered!
#2

[eluser]codex[/eluser]
Maybe I'm wrong, but what you seem to be doing in hello() is just setting a variable and not returning a value.

I think you should be doing this, although it's not entirely clear what you're after:

Code:
function hello() {
        $data['message'] = 'Hi there!';
return $data;
    }
#3

[eluser]eedfwChris[/eluser]
The variable $data is still in it's own function scope...
You would do this:
Code:
class Test extends Controller {
    function Test()
    {
        parent::Controller();
        $this->load->helper('my'); // according to the User Guide, I don't have to tell CI where to find this file, or show the "_helper.php" part.
    }

    function index()
    {
        $data['testing'] = "Testing this out.";
        $data['message'] = hello();
        $this->load->view('test', $data);
    }
}

application/helpers/my_helper.php
Code:
function hello() {
        return 'Hi there!';
    }
#4

[eluser]tinawina[/eluser]
Just one more tidbit on this. If I change my_helper.php to include an echo statement:

Code:
function hello() {
   echo 'hello for the heck of it...';
   $data['message'] = 'Hi there!';
}

I do see "hello for the heck of it..." , just before testing this out. I guess I tried this just to make sure the my_helper.php file was ok. Smile Thanks!
#5

[eluser]Michael Wales[/eluser]
Do what athfar said - that should help you out.

You may also want to print_r the $data variable and ensure CI isn't doing anything funky with your variables t1hat you aren't expecting.
#6

[eluser]tinawina[/eluser]
Thank you Codex and Athfar for the speedy replies!

Athfar - your solution did the trick. I tried yours Codex, but continued to receive that error.

Also Codex - I really was only after a little foundation knowledge. I knew this had to be simple - but couldn't get it to work at all. Of course, now I will try to apply this new-found knowledge to a real application I'm working on. I'm sure I'll be posting again soon....

WalesMD - I did try a print_r and got: Array ( [testing] => Testing this out. [message] => hi there! ) So all is working properly now. Thanks for the tip.

Thanks again and have a great day!
#7

[eluser]codex[/eluser]
[quote author="tinawina" date="1187138623"]Thank you Codex and Athfar for the speedy replies!

Athfar - your solution did the trick. I tried yours Codex, but continued to receive that error.

Also Codex - I really was only after a little foundation knowledge. I knew this had to be simple - but couldn't get it to work at all. Of course, now I will try to apply this new-found knowledge to a real application I'm working on. I'm sure I'll be posting again soon....

Thanks again and have a great day![/quote]

I should have looked better, then I probably would have seen that this wasn't going to work. But hey, I'm also learning! ;-)
#8

[eluser]Phil Sturgeon[/eluser]
Most people are against the use of global, but...


Code:
function hello() {
        $_GLOBAL['data']['message'] = 'Hi there!';
    }

That WOULD do it, but dont :p
#9

[eluser]John_Betong[/eluser]
Hi,
&nbsp;
The way I understand this is a learning experience for OOPS:
&nbsp;
1. Test and include a helper file.

2. Set change the $data['message'] in the "my_helper.php" file

3. Output the results in a view file.
&nbsp;
&nbsp;
So...
&nbsp;
The naming, linking the my_helper file is OK but the function hello() is oblivious of the $data array and coughs up the following: "Message: Undefined variable: message"
&nbsp;
&nbsp;

One way of solving this as suggested by pyromaniac is to make the variable global throughout your program:
function hello() {
$_GLOBAL['data']['message'] = 'Hi there from my_helper->hello()';
}
&nbsp;
&nbsp;

A second method is to make the whole $data array global:
function hello() {
global $data;
$data['message'] = 'Hi there from my_helper->hello($data) ';
}
&nbsp;
&nbsp;

A third method is to pass the $data variable as a VALUE parameter AND return the amended $data array:
function hello( $data ) {
$data['message'] = 'Hi there from my_helper->hello($data) ';
return $data;
}
&nbsp;
But using the above method in your Controller you will have to change the following line:
$data = hello($data);
&nbsp;
&nbsp;
&nbsp;


A fourth method is to pass the $data as a REFERENCE parameter (which does not require a return value).
function hello( $data ) {
$data['message'] = 'Hi there from my_helper->hello($data) ';
}
AND in your Controller to change the passed $data array by REFERENCE:
hello( $ $data );

&nbsp;
That has woken me up, time for my second coffee and back to the grind stoneSad
&nbsp;
Cheers,
&nbsp;
John_Betong
#10

[eluser]tinawina[/eluser]
Ok - thanks everyone for all of the help! Now......I have a follow up that expands on this topic.

I have a bunch of yes/no questions that I want site visitors to click through to get to a final answer. I am using the suggestion posted in this thread -- http://ellislab.com/forums/viewthread/58510/ -- with some modifications. I'm running into this error message when I take a look at my questions.php file in a browser: "Fatal error: Call to a member function on a non-object in [the helper file] on line 3".

Here's where I'm at with this:

Question Config File (config/questions.php)

Code:
$config['question']['1'] = "This is the text for question one.";
$config['question']['2'] = "This is the text for question two.";
$config['question']['3'] = "This is the text for question three.";


Controller (questions.php)

Code:
class Questions extends Controller
{

    function Questions()
    {
        parent::Controller();
        $this->load->config('questions');
        $this->load->helper('questions');
    }

    function index()
    {
        $data['question'] = getquestion();
        $this->load->view('question', $data);
    }
}


Helper file (application/helpers/questions_helper.php)

Code:
function getquestion($number) { //in the end I want to be able to pass a question number to this function in order to get the next question on the screen
    $questions = $this->config->item('question'); //<-- this is the line referred to in the error message
    foreach($questions AS $key => $value)
    {
        if ($key == $number)
        {
            return $value;
        }
    }
}


The view (questions.php)

Code:
&lt;html&gt;&lt;head&gt;&lt;title&gt;test&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
<p>&lt;?php echo $question; ?&gt;</p>
&lt;/body&gt;
&lt;/html&gt;

I put this together yesterday afternoon and I've been searching around for an answer and modifying/testing this code ever since. I just can't figure out what I'm doing wrong. I have a feeling it's something totally obvious, but what? Again, any help is greatly appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB