Help: Adding variables to include() [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Holiday
Forum Newbie
Posts: 2
Joined: Thu Jul 30, 2009 6:07 pm

Help: Adding variables to include() [Solved]

Post 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
Last edited by Holiday on Fri Jul 31, 2009 1:03 am, edited 1 time in total.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Help: Adding variables to include()

Post 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.
joeynovak
Forum Commoner
Posts: 26
Joined: Sun May 10, 2009 11:09 am

Re: Help: Adding variables to include()

Post 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
Holiday
Forum Newbie
Posts: 2
Joined: Thu Jul 30, 2009 6:07 pm

Re: Help: Adding variables to include()

Post by Holiday »

Cool.. That's excatly what I was looking for
Thanks
Post Reply