Ok, so I am a total PHP n00b so I will explain my problem then say how I would personally logically solve it.

I am creating a website using a php include at the top of the page (same for every page on the site) and one for the bottom of the page. Basically what I want to do is have (what I call) a tree at the top of my page inside my header include file. By tree I mean something like "Home> Tutorial> Html Tutorial > using tables Tutorial". This would of course change based on the page that is being viewed.

My logical solution:

Instead of my tree text in pure html I would have something like "home + Variable". The variable would then be specified at the top of my html file that is the content for the page.

I hope that all makes sense. I would be really grateful if someone could tell me how to do this using PHP. Thank in advance

Recommended Answers

All 11 Replies

i'm very interested. i'm a noob too, but i think i can do it. but i'm a little baked right now and jamming TOOL. so i'll get back to you later tonight for sure. very interesting.

I really need help with this. If anyone can help me I would be very grateful!!!

Hi guys,

I still haven't found an answer for this and it is vital for my site. I would be so grateful for any response. I will give a gmail invite to anyone who help me, I promise!!!

ok here we go:

header.php

<html>
<body>

Home > <?php echo $variable; ?>

page.php

<?php

$variable = "Current Page";
include "header.php";

?>

I hope that helps you out =)

You had me going for a bit but I have cracked it. It wasn't that you were wrong it was that I didn't realise that you had done the include for me too. Thank you very much mate excellent. Please see the gmail invite in your inbox!

Another question related to this:

What happens if i want to turn all the parts of my tree into hyperlinks which will need to be from http:// upwards. Eg.

<?php
 
$variable = ("<a href="http://www.mysite.co.uk/tutorials/">Tutorial</a>&gt;
<span><a href="http://www.mysite.co.uk/tutorials/photoshop/">Photoshop Tutorial Home</a></span>");

include ("includes/header.php");
 
?>

Basically what happens is the tree goes through each page it is logical to go through to get to the current page and puts them as links and then has the page you are currently on inside span tags which I have used to make the text larger.

any help would be gratefully recieved.

if you arange your folders like a tree, and have each page as the folder index... then you can just link relatively to the folder above*, like this: "../". if you don't arrange your files into a neat and logical tree, i'd advise that you do.

*above being towards the http:// in matt-syntax

Yes obviously but!

Because my folders are done nice and neatly and I do only have one header.php all the links must be done down to http:// otherwise when the html is inserted into the content php the links will break depending on which folder they are in. I hope this makes sense.

Still looking for an answer.. Anybody?

hmmm, not sure I understand that? you should be able to include a file from the document root directory, by preceding it with a slash, ie:

include ("/includes/header.php");

... maybe that will go to your "home" directory though...

EDIT: Well, on my server, / goes to the server root folder. So, you'll need to know the path from the server root folder to your document root (public_html) folder. You can find that using:

<?php echo getcwd() ?>

For me, that's /home/fusion/public_html/(+ the path to the folder where the echo getcwd script is running in).

So, to get a file from the "includes" folder in my document root in PHP, I'd use:

include ("/home/fusion/public_html/includes/header.php");

This will be different for your server, and it may even be based from a "C:\" drive if you're hosted on a Windows flavour...

There is an environment variable you can use to find the server's document root I think, don't know which one, but it's probably DOCUMENT_ROOT... =)

EDIT (2): You should use absolute hrefs if your header includes ok but the links are 'broken'. Or, write some complex logic ^_-

Complex?

As long as your page won't be changing relative locations, just note how far it is in the directory structure and write a for loop to add that many "../" to a variable.

$DEPTH=3;

include /file/

$base = "";
for ($i = 0; $i < $DEPTH; $i++)
  $base = "../" . $base;

The above code is NOT tested, its purpose is to communicate the idea, not to be valid code.

That's how I solved that problem for myself.

well, providing a depth is more complex than always importing the same piece of page and expecting it to link relatively (and correctly).

you could even work out the depth automatically from the environment vars.

either way; you may run into a problem if the site has subdomains. ^_-

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.