Correct way to use homemade functions |
[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() { Here's my controller file: application/controllers/test.php Code: class Test extends Controller { And here's my view file: application/views/test.php Code: <html> 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!
[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() {
[eluser]eedfwChris[/eluser]
The variable $data is still in it's own function scope... You would do this: Code: class Test extends Controller { application/helpers/my_helper.php Code: function hello() {
[eluser]tinawina[/eluser]
Just one more tidbit on this. If I change my_helper.php to include an echo statement: Code: function hello() { 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. Thanks!
[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.
[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!
[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! ;-)
[eluser]Phil Sturgeon[/eluser]
Most people are against the use of global, but... Code: function hello() { That WOULD do it, but dont :p
[eluser]John_Betong[/eluser]
Hi, The way I understand this is a learning experience for OOPS: 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. So... 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" 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()'; } A second method is to make the whole $data array global: function hello() { global $data; $data['message'] = 'Hi there from my_helper->hello($data) '; } 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; } But using the above method in your Controller you will have to change the following line: $data = hello($data); 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 ); That has woken me up, time for my second coffee and back to the grind stone Cheers, John_Betong
[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."; Controller (questions.php) Code: class Questions extends Controller 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 The view (questions.php) Code: <html><head><title>test</title></head> 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. |
Welcome Guest, Not a member yet? Register Sign In |