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!


Messages In This Thread
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:27 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:34 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:36 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:37 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:42 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:43 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 01:45 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 02:17 PM
Correct way to use homemade functions - by El Forum - 08-14-2007, 08:58 PM
Correct way to use homemade functions - by El Forum - 08-15-2007, 01:08 PM
Correct way to use homemade functions - by El Forum - 08-15-2007, 01:51 PM



Theme © iAndrew 2016 - Forum software by © MyBB