Forum Moderators: coopster

Message Too Old, No Replies

How to include a file using a variable?

         

mytah

12:44 am on Jan 23, 2009 (gmt 0)

10+ Year Member



I want to make a template type php site and want to keep the header, footer... as separate files and call them into the page

Here is my issue I want to call them with a variable defined and held in a different file (config.php) then simply include the config.php file and anytime I use one of the variables like for instance {header}
the header file is included in the html

Thanks for any help on this!

mack

12:08 am on Jan 25, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Sorry I may have picked this up wrong. Are you tryign to enable the user to switch layouts by switchign the header and footer files?

You could do this in a number of ways. You mentioned you want to use a config file... Within that file you could do somethign like..

in this example this file would be layout.php


<?php
if ($layout =='1')
{
$header="header1.php";
$footer="footer1.php";
}

if ($layout =='2')
{
$header="header2.php";
$footer="footer2.php";
}

else
{
$header="defaultheader.php";
$footer="defaultfooter.php";
}
?>

Then on your actual page use somethign like the following...


<?php

$layout=$_GET['layout'];
include"layout.php";

include"$header";
echo"main content";
include"$footer";
?>

Then to set a layout you woudl pass the valuw in the url. www.example.com/page.php?layout=1

first we use $_GET to get the value from the url, we then include the layout.php file. the value will then be passed though this file and this will set the layout values. then when we include header and footer within the main file we are using a variable obtained from layout.php

Hope this gives you some ideas.

Mack.

mytah

12:41 am on Jan 25, 2009 (gmt 0)

10+ Year Member



Thank you for that but no that is not what I am looking for

what I am looking for is this:

I have a "home.php" file This is the file I am calling in the browser how ever the home.php is calling a home.html file that has the following:

{header}

***content****

{footer}

The {header} & {footer} variables include the header/footer into the final home.php which is being displayed in the browser

My question is how do I include the header & footer in that format instead of using the include statement in every file

Thank you for all your help!

eeek

4:42 am on Jan 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



instead of using the include statement in every file

What's wrong with using include files?

mytah

5:11 am on Jan 25, 2009 (gmt 0)

10+ Year Member



I just want to use a variable instead of an include statement

willybfriendly

5:36 am on Jan 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$header =<<<EOF
<!--header HTML goes here -->
EOF;

$footer =<<<EOF
<!--footer HTML goes here -->
EOF;

echo $header;
echo $page;
echo $footer;

Is it something like that?

mytah

5:58 am on Jan 25, 2009 (gmt 0)

10+ Year Member



yes however I would like to store the variable values in a config.php file separate from everything

So I would have a "config.php" file that sets the values of the variables then I could only include that file and call the variables as I need them

Thanks for all your help!

mack

1:20 pm on Jan 25, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Why not just create a config.php file and place the v alues in there, then include config.php right into the files you weant to use the layout with.

Do you want to have multiple config files to anable different layouts for files?

if so you could use one config file with conditions

Mack.

mytah

5:04 pm on Jan 25, 2009 (gmt 0)

10+ Year Member



Could you give me an example?

Thanks

mack

5:30 pm on Jan 25, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Very similar to my code example above. In this case the file i created (layout.php) would be your config file. In there you can specify the html you want the header files to contain.

Then within your page simple include the config file and specify a value

include"config.php";
$Value="1";

I wouldent try to pass the value with the include because the chances are this will not work...

include"config.php?value=1"; there is no reason to pass the value to the include because it will be placed inlibne with the main file and the value will be read in that way.

Another very rough way of doing this would be to place the html code for the header and footer file within a text file and name them header1.php header2.php ect ect

then from within your config file include the text files to generate the html within the file. then when the value is passed it will render the html code from the text files.

Perhaps if you provide a little bit more information about exactly what you are trying to acheive we may be able to opffer you some better ideas. Right now the suggestions you are receiving are very general.

Mack.