Forum Moderators: coopster

Message Too Old, No Replies

PHP Code inside of PHP Code

         

bubbamagic87

9:45 pm on Mar 10, 2006 (gmt 0)

10+ Year Member



I'm using PHP includes and CSS for my website. Rather then having to change the directory of ever include and CSS location on each page, I would like to only change one line.

Heres what I want to change:

<link rel="stylesheet" type="text/css" href="<?=$location?>web.css" />
<?php include("<?=$location?>header.php");?>
<?php include("<?=$location?>footer.php");?>

Where $location equals something like "../../".

Obviously you can use PHP inside of PHP. Is there another way to do this?

coopster

10:04 pm on Mar 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, bubbamagic87.

You could set the variable at the top of your script. Or perhaps message #17 in this PHP Forum Library [webmasterworld.com] thread on Good PHP solutions to small problems [webmasterworld.com] will give you some ideas.

bubbamagic87

10:27 pm on Mar 10, 2006 (gmt 0)

10+ Year Member



Sorry, I'm very new to PHP.

How would make the variable?

It would be like:

$header = $location...

but I don't know how to add 'header.php' onto that.

inveni0

10:37 pm on Mar 10, 2006 (gmt 0)

10+ Year Member



The only way to do what you want is like:

$location = "../../";
<?php include($location."header.php");?>
<?php include($location."footer.php");?>

See, that requires you to define $location on each page. I would store the value of $location in a database or text file. Then, you can query the db or read the text file to get the $location string. If you ever needed to change the value of $location, you'd only need to edit one database entry or text file.

bubbamagic87

10:47 pm on Mar 10, 2006 (gmt 0)

10+ Year Member



Thanks alot.

Is there a way to automatically get the $location value based on where the file is?

So 'index.html' would have a $location = nothing but 'folder1/folder2/page.html' will have a $location = '../../'.

Is there a script that can figure this out based on folder depth?

inveni0

10:51 pm on Mar 10, 2006 (gmt 0)

10+ Year Member



If I'm understanding correctly, you want the $location to change between each file, relatively...

If I were you, I'd place:

[yoursite...] (your ABSOLUTE url)

into a text file and read it with each page.

henry0

11:18 pm on Mar 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could do
<<<

require_once($_SERVER 'DOCUMENT_ROOT']."/header.php");
where header is set relative to root

>>>

as such you may include the header wherever regardless
ot its full path

Coopster has a nice include set once for all in our library
I cannot remember where it is
but Coops if you read this...
thanks