Welcome Guest, Not a member yet? Register   Sign In
Getting values from an external include file
#1

[eluser]Allen Dino[/eluser]
Hi everyone!

I've been using CI for the past few weeks and it's great!

I need to get a variable from an external file (not located in the CI folder and can't be moved).

This is my app structure:
<root>/index.php
<root>/ci/<everyting CI here, didn't change default settings>

Code in my index.php (I just made this simpler):
Code:
&lt;?php
$a = "value";
?&gt;

Code in one of my controllers:
Code:
function index(){
    include ("http://<root>/index.php");
    echo $a;
}

When I run the application, I get a "Undefined variable: a" error. Why can't CI retrieve the $a variable when I "legally" included the <root>/index.php in the index of my controller. Are there any CI rules I am breaking here? Please remember that I can't move the <root>/index.php file.

Thanks for the help in advance and I hope I do make sense!
#2

[eluser]Jelmer[/eluser]
Take a look at the config class in de user guide.

Either add your own config file and include it in the application/config/autoload.php or add your values to the application/config/config.php as:
Code:
$config['a'] = 'something';

Both these options will allow you to access your values by using:
Code:
$this->config->item('a')
#3

[eluser]Allen Dino[/eluser]
Hi, thanks for the reply.

I think you misunderstood me. The <root>/index.php CAN'T be moved. I am forced to include the file as it is since this file is previously developed already and I am just adding a few functionalities of my own via CI, but some of the variables I need are inside the <root>/index.php
#4

[eluser]Jelmer[/eluser]
Ah, I see, I assumed the index.php was your CI index.php.

So you'd like to use variables set in your own script to be accessable in CI? And I'm assuming that file has some functionality of its own that you wouldn't want loaded when in your CI application?

In that case I would put the settings from your index.php in another file which you include both in your index.php and in the controler from CI in which you need it...
#5

[eluser]drewbee[/eluser]
The problem is scope.

Your $a variable does not exist inside of the function. One quick and dirty way would be to global the variable IE

Code:
function index(){
    global $a;
    include ("http://<root>/index.php");
    echo $a;
}

http://us2.php.net/global
#6

[eluser]Allen Dino[/eluser]
[quote author="Jelmer" date="1225398821"]Ah, I see, I assumed the index.php was your CI index.php.

So you'd like to use variables set in your own script to be accessable in CI? And I'm assuming that file has some functionality of its own that you wouldn't want loaded when in your CI application?

In that case I would put the settings from your index.php in another file which you include both in your index.php and in the controler from CI in which you need it...[/quote]

Hi Jelmer,

I'm a bit confused with your reply. Can you give an example please?
#7

[eluser]Allen Dino[/eluser]
[quote author="drewbee" date="1225399050"]The problem is scope.

Your $a variable does not exist inside of the function. One quick and dirty way would be to global the variable IE

Code:
function index(){
    global $a;
    include ("http://<root>/index.php");
    echo $a;
}

http://us2.php.net/global[/quote]

Hi drewbee, and thanks for the suggestion. Sadly, I did what you suggested but the echo line will not show the value assigned in the index.php. Although the global scope removed the error.
#8

[eluser]drewbee[/eluser]
Hmm that variable should be available within the function without the global. The global actually makes it available *outside* of the function.

I am starting to think the file is not being included properly. Can you put an echo statement with the index.php file and see if it outputs from your index() function being run?
#9

[eluser]Allen Dino[/eluser]
[quote author="drewbee" date="1225401995"]Hmm that variable should be available within the function without the global. The global actually makes it available *outside* of the function.

I am starting to think the file is not being included properly. Can you put an echo statement with the index.php file and see if it outputs from your index() function being run?[/quote]

Hi drewbee,

I followed your instructions, and added an extra include also, and here's what I did.

Code in controller (controller located in default controller folder):
Code:
function index() {
    global a$;
    global b$;
    include ("<root>/index.php");
    echo ("This is a (" . $a . ") echoed in this controller");
    echo ("This is b (" . $b . ") echoed in this controller");
}

Code in index.php:
Code:
&lt;?php
    include ("<root>/another_include.php");    
    
    $a = "string a";
    
    echo ("<br />This is a (" . $a . ") echoed in index.php");
    echo ("<br />This is b (" . $b . ") echoed in index.php");
?&gt;

Code in another_include.php:
Code:
&lt;?php
    $b = "string b";
    echo ("<br />This is b (" . $b . ") echoed in another include");
?&gt;

Output when controller is invoked:
Code:
This is b (string b) echoed in another include
This is a (string a) echoed in index.php
This is b () echoed in index.php
This is a () echoed in this controller
This is b () echoed in this controller

This is driving me nuts! :ahhh: Each of the include files only display the variables when echoed on their own file, but when the variables are echoed outside, nothing shows.

Is there any CI config that I should check? I think CI does something with the included files that prohibits the controller from getting the included variables, but I can't figure out how to check this.
#10

[eluser]Allen Dino[/eluser]
Hi All,

Here's an interesting find...

Instead of absolute paths in the includes, I converted them to relative paths and lo and behold, it worked! What's wrong with including absolute paths anyway? The absolute paths I used were using the CI site_url() function.




Theme © iAndrew 2016 - Forum software by © MyBB