Jump to content

Organising Include Files


eiranix

Recommended Posts

I want to use the php include function to apply the header navigation to each page of my site from a single file.

How do you deal with pages that are within sub directories?

 

For example, I have a News page that sits in the root folder but then all the news items are within a 'news' folder.

This means all the relative nav links and image references on news item pages need '../' before them.

 

Do I have to create a second menu file for subdirectories or is there a way around this?

Link to comment
Share on other sites

That looks helpful for the file you are including but what I meant was the links inside the file you are including.

If the file you are including is the navigation menu, do I need to use a separate menu file when it's included on pages that are stored in a sub directory?

 

In this example I want the menu included on all six pages, but 3 of them are in the folder called 'news'. This means the links in the menu won't work:

 

/menu.php

 

/home.php

/news.php

/contact.php

/news/item1.php

/news/item2.php

/news/item3.php

 

Or am I getting this wrong, does the included file base it's links off the location of the menu file or the location of the page the menu is included on?

Link to comment
Share on other sites

No you use just how you have been doing, adding include link relative to the document using ./menu.php ( which takes it out of news folder to the root), most people make the mistake of adding <html><body><head> with links to css and js files etc, but all is required is the actual menu html code, when this will used from the main document the menu is inserted in.

Link to comment
Share on other sites

Ok cool.

So back to the original question - Is there a way using php or perhaps javascript to have the menu detect that it is currently being applied to a page in a sub directory and adjust the menu's links accordingly?

Link to comment
Share on other sites

in the menu links you include the full path with the first slash representing your root directory, when this is included in any pages, as long you start from root the links should tally with folder locations.

<li><a href="/home.php">home</a></li>        <li><a href="/contact.php">contact</a></li>        <li><a href="/news.php">news</a></li>        <li><a href="/news/item1.php">item1</a></li>        <li><a href="/news/item2.php">item2</a></li>        <li><a href="/news/item3.php">item3</a></li>

if your root directory is localhost when you hover over links you would see 'localhost/home.php', 'localhost/news.php', 'localhost/news/item1.php

Edited by dsonesuk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...