Jump to content

Recommended Posts

Hi,

 

I'm in the process of creating a template with includes, and am a little bit of a PHP newbie, so this may be a simple question. But the context is kind of complex.

 

Here's the question:

 

Is there a way for the relative content within php includes to show up fine in various php files in different-leveled folders, much like relative content within css files does?

 

Let me explain.

 

I have a Web site that has many levels, ex:

 

----

mywebsite/testpage0.php

mywebsite/child1/testpage1.php

mywebsite/child1/child2/testpage2.php

----

 

You get the idea.

 

I also have an include:

mywebsite/_includes/include1.php

 

I want to create an include with relative links and images and such, so that when I reference this include from another php file (testpage0.php, testpage1.php, testpage2.php, etc), no matter how far down these testpage php's are, they still get to the correct links and images.

 

This principle works fine with CSS:

 

Say i have a css file, located in

mywebsite/_css/styles.css

 

If I link each testpage.php page to a css file, as if they were like xhtml pages, that would be no problem within the head tags:

 

testpage0.php (mywebsite/testpage01.php)

...

<head>

<link href="/_css/styles.css" rel="stylesheet" type="text/css" />

</head>

...

 

testpage1.php (mywebsite/child1/testpage1.php)

...

<head>

<link href="../_css/styles.css" rel="stylesheet" type="text/css" />

</head>

...

 

testpage2.php (mywebsite/child1/child2/testpage2.php)

...

<head>

<link href="../../_css/styles.css" rel="stylesheet" type="text/css" />

</head>

...

 

Similarly, if I were to put includes in each testpage page, that would be no problem, too.

(For reference, I'm using the @ require_once code from A List Apart: http://www.alistapart.com/articles/phpcms/ )

 

----

 

include located at mywebsite/_includes/include1.php

 

testpage0.php (mywebsite/testpage0.php)

<?php @ require_once ("/_includes/include1.php"); ?>

 

testpage1.php (mywebsite/child1/testpage1.php)

<?php @ require_once ("../_includes/include1.php"); ?>

 

testpage2.php (mywebsite/child1/child2/testpage2.php)

<?php @ require_once ("../../_includes/include1.php"); ?>

 

----

 

and so on. That part is no problem. It's a relative path to the include depending on how far deep the testpages are buried.

 

But here's where CSS and php differ, and is in essence my question.

 

When I link to the css file, I can put relative file paths in the css file itself, like background images. Ex:

 

background in mywebsite/_pics/background.jpg

 

styles.css (mywebsite/_css/styles.css)

#samplediv{

background-image:url(../_pics/background.jpg);

width:100px;

height:100px;

}

 

As long as I link to background.jpg correctly in styles.css, and I link to styles.css correctly in testpage.php, it doesn't matter how far down any given testpage is. The background file will still show up correctly.

 

But if I do something similar with an include:

 

----

 

include.php (still bascially an xhtml file, located in mywebsite/_includes/include.php)

...

<img src="../_pics/background.jpg" />

...

 

-----

 

...the server side takes the relative link literally and just writes the code as that exact relative path. In this example background.jpg will show up fine ONLY from the 1st child level, which would be testpage1.php:

mywebsite/child1/testpage1.php

 

It won't link properly from deeper in:

mywebsite/child1/child2/testpage2.php

(will instead only go up one folder ../ and try to find mywebsite/child1/_pics/background.jpg , which doesn't exist)

 

And I don't believe it will work farther up either (mywebsite/testpage0.php will link to _pics/background.jpg, I'm guessing)

 

So....

 

Is there any way I can make the relative paths *WITHIN the includes* themselves work so that when everything is finally written server side, it doesn't matter how far deep in the testpages are?  Can I in other words have an include work more like a CSS file, where a relative link (images, links) within a relative link (include) from the testpage.php page still works no matter where it is?

 

Or is there a better way to organize this info? Oh, and I'm opting against absolute links, because I'm working on a proof site with a different url before transferring it live, meaning ALL links will have to be rewritten if I go the absolute route. Can't do that. Or maybe there are other alternatives?

 

Hope this stuff made sense. Please let me know if you have any questions. Any help will be really appreciated!

 

rcrider

 

 

 

 

 

if i read this right your trying to find files no matter how far down the tree.

 

in that case id use while with file_exists:

 

$breaker=0;
$inc_file = 'includes/myfile.php';
while(!file_exists($inc_file)){$inc_file = '../'.$inc_file;$breaker++;if($breaker>=20){break;}}

 

hopefully helpfull. :)

Actually, I'm trying to find files within the php include (from another php file that references that include), not really the php include itself. Although, if I move the referencing testpage php files around, it's good to know that that code exists to find the includes, so thanks for that heads up.

 

Basically I'm looking for a way, to try and condense my lengthy explanation, for

 

mywebsite/testpage0.php

mywebsite/child1/testpage1.php

and

mywebsite/child1/child2/testpage2.php

 

to find mywebsite/_pics/background.jpg

through the include mywebsite/_includes/include1.php

 

The challenge for me is to have the background.jpg file show up properly on all these multi-level testpages (child level, grandchild, etc), without having to resort to absolute links or creating different includes for each level. I can go those other two routes, but I'm hoping there's another way.

 

The real site will obviously be much more complicated. For example, include1.php is really several--a header, a Web accessible dropdown, a footer--each of those in different levels. And each of those includes will, within those files themselves, link to several pictures and site links as if the includes were parts of xhtml files. Similarly, testpage0.php, testpage1.php etc is tens to hundreds of uniquely named files. I'm organizing the site in a template format to separate very large, complicated xtml-like pages, take any repeating elements (like headers and dropdowns) and make them into their own includes, and then get more intricate within the includes themselves like they were separate files. But it seems like with the various levels that this is becoming harder than anticipated!

 

Or am I as a PHP newbie going about this all wrong? I'm sure I'm not the only one who has run into this problem, since it seems like one of the strengths of php (or any language with includes) is to create a template structure and separate the content out with includes. It's when I get to the relative links combined with the multiple levels that I'm running into problems.

 

Hope that made more sense!

 

Thanks.

 

 

Absolute links are the way to go. You can generate them like this:

 

<?php
define("HOST", "http://" . $_SERVER['HTTP_HOST'] . "/");
$folder=""; // the subdirectory the main script is in if any
define("IMG_INC", HOST . "images/");
?>

 

Then just use IMG_INC in your CSS file like so

 

<?php 
include constants.php
?>
.style {
     background= url(<?php echo IMG_INC; ?>background.jpg);
}

 

You would need to change .css to .php but that is much easier to do and is dynamic with no guess work. That is my opinion anyhow.

Ok, I think I figured it out, thanks to both of your answers regarding variables and concatenation (again, I'm a newbie!).

 

Created a variable, say $dirpath, which will be different for each leveled testpage.php

I can first use this variable and concatenate with the rest of the path to find the include file

 

-------

 

mywebsite/testpage0.php

... (putting within body tag like an xhtml file)

<?php $dirpath="";

@ require_once ($dirpath . "_includes/include1.php"); ?>

...

 

mywebsite/child1/testpage1.php

...

<?php $dirpath="../";

@ require_once ($dirpath . "_includes/include1.php"); ?>

...

 

mywebsite/child1/child2/testpage2.php

...

<?php $dirpath="../../";

@ require_once ($dirpath . "_includes/include1.php"); ?>

...

 

-----

 

Next I can use that same $dirpath variable in the include file to link correctly to each relative image or text link:

 

-----

 

mywebsite/_includes/include1.php

<img src="<?php echo $dirpath; ?>_pics/background.jpg" />

<a href="<?php echo $dirpath; ?>child1/page1.php"> Sample link </a>

etc.

-----

 

Once $dirpath is defined, every relative path falls into place. I tested this and background.jpg shows up great on all three testpages!

 

Now, quick question. The part that worries me is that I have TONS of links and images in this so-called include1.php. Meaning I'm going to put in a lot of:

<?php echo $dirpath; ?>

 

Like hundreds.

 

Will this slow the web page down significantly, you think, having to go into php mode so many times? Or will hundreds of links and images still happen in a blink of an eye? Fast performance is definitely important.

 

Thanks again!

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.