Including menu.php in a pages in some other sub directories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Charles_M
Forum Newbie
Posts: 1
Joined: Sun Mar 17, 2013 6:05 pm

Including menu.php in a pages in some other sub directories

Post by Charles_M »

I am learning PHP. I want to create a menu that can open pages from any folders in the site's directory irrespective of whether it is in a ./sub/sub/sub/directory. The following is the structure of the directories and files:
ROOT DIRECTORY
dirINCLUDES
header . php
footer . php
navigation . php
dirPROJECT
index . php
project1 . php
project2 . php
index . php(ie, homepage)
The following is in my navigation.php:
<ul >
<li><a href = "index.php" > home </a></li>
<li><a href = "project/index.php" > project </a></li> </ul>
Right now when I click on the "project" link, it takes me to the project/index.php, but when I am on that page and click on the"home" link it doesn't take me to the home page rather, it giving me the current path of that subdirectory which am in. I want to change the paths name to what it suppose to be.
Any help will be much appreciated. Thank you.
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Including menu.php in a pages in some other sub director

Post by mecha_godzilla »

Hi,

The easiest way to do this is to define a constant representing your site root and generate full paths to these pages in your menu links - you can either use the define() method for this purpose or (for OOP implementations) create a new object property that your application can access in the menu scripts. From the site user's perspective the links won't look any different because the browser usually displays the full path for each link anyway even if the link is only defined as a local reference within the HTML, and you only have to hard code the full path in one place - most frameworks and open source applications that I've looked at use this method, in addition to whatever URL routing controls or rewriting they do for permalinks, etc.

HTH,

Mecha Godzilla
Post Reply