Page 1 of 1

Help: Adding variables to include() [Solved]

Posted: Thu Jul 30, 2009 11:07 pm
by Holiday
I have a file called index.php that has a table with columns in it. For simplcity, let's say there are only 2 columns and let's call them column A and column B.

Column A and column B are both supposed to show some data. The layout of the data (text fields, etc.) is supposed to be the same for both columns, but the data is supposed to be different.

For example, column A and column B both show a first name and a last name, but the names are different.

I also have another file called content.php which has the layout for all the fields of the data.

What I have done is added the line of code

Code: Select all

<?php include("./content.php")?>
to all the columns.

My question is, how can I pass in some sort of variable into that include so I can tell content.php how to behave?


Thanks

Re: Help: Adding variables to include()

Posted: Fri Jul 31, 2009 12:12 am
by omniuni
Right before calling the include set your $_GET['key'] = 'value'; and you can then access that from within your included file. Really, it works the same way if you were to just create $variable = 'value'; but it can be useful to write a file that uses $_GET so you can use it outside of this one application and on its own.

Re: Help: Adding variables to include()

Posted: Fri Jul 31, 2009 12:13 am
by joeynovak
Hi Holiday,

When you include a file in php, it has access to most (and I would even say ALL, but I don't know everything and their may be an exception) of the same variables that the page you "included" it from has. It also has access to ALL the super globals (super globals are the global variables that are ALWAYS around (that's why their SUPER) like $_POST, $_GET, $_COOKIE, $_REQUEST, $_SESSION, etc...). 8)

So, the following will work:

b.php

Code: Select all

 
<?php
go();
 
function go(){
    $a = 100;
    include('a.php');
}
?>
 
a.php

Code: Select all

 
<?php
echo $a;
?>
 
Good luck!

Joey

Re: Help: Adding variables to include()

Posted: Fri Jul 31, 2009 1:00 am
by Holiday
Cool.. That's excatly what I was looking for
Thanks