Page 1 of 1

Include menu for subdirectory

Posted: Wed Jun 25, 2008 9:35 am
by camkorn
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello.

I have set up a php include navigation menu for my website. It works great, apart from on my subdirectories. I have include the file in these sub direcotries using:

Code: Select all

<?php  include ('../includes/menu.inc.php');?>
The menu consists of the following code:

Code: Select all

<?php $currentpage = basename ($_SERVER ['SCRIPT_NAME']);?>
<ul>| 
<li><a href="index.php" <?php if ($currentpage == 'index.php') { echo 'class="current"'; }?> >Home </a></li>| 
 
<li><a href="services.php"<?php if ($currentpage == 'services.php') { echo 'class="current"'; }?> >Services</a> </li> | 
 
<li><a href="website_design_hertfordshire.php"<?php if ($currentpage == 'website_design_hertfordshire.php') { echo 'class="current"'; }?> >Website Design</a> </li>| 
 
<li><a href="ecommerce_websites_hertfordshire.php" <?php if ($currentpage == 'ecommerce_websites_hertfordshire.php') { echo 'class="current"'; } ?>>Ecommerce Solutions</a></li>|  
 
<li><a href="portfolio.php" <?php if ($currentpage == 'portfolio.php') { echo 'class="current"'; }?>>Portfolio</a></li>|
 
<li><a href="quote.php" <?php if ($currentpage == 'quote.php') { echo 'class="current"'; }?>>Quotation</a></li>| 
</ul>
Of course the links don't work because the subdirectory doesn't know where index.php is for instance.

Is there anyway to get the script to recognise when it is being called in a sub directory? Any pointers in the right direction would be very much appreciated.

Regards,

Clare Green.


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Include menu for subdirectory

Posted: Wed Jun 25, 2008 2:41 pm
by JAB Creations
Try this...

Code: Select all

include realpath(dirname(__FILE__) . "/../folder/file.php");

Re: Include menu for subdirectory

Posted: Wed Jun 25, 2008 4:47 pm
by camkorn
Hello.

Thanks so much for taking the time to reply but I think I was probably unclear in what I was asking.

The menu is contained within an include file that is located in the root of my website in a folder called includes.

I insert the include file in to the pages of my root and it appears correctly and the links work correctly as they are written as index.php portfolio.php etc

When I used the same include file in webpages that are located in sub directories the include appears correctly but because the links are wrtiten as index.php etc they come up as page not found due to the fact the include file is being called from a file that is located in a subdirectory.

I have had a play about and have tried the following modifications to the include file:

Code: Select all

 
<?php
 
$currentDirectory = basename(dirname(__FILE__));
if ($currentDirectory == 'camweb') { 
$currentpage = basename ($_SERVER ['SCRIPT_NAME']);
?>
<ul>| 
<li><a href="index.php" <?php if ($currentpage == 'index.php') { echo 'class="current"'; }?> >Home </a></li>| 
 
<li><a href="services.php"<?php if ($currentpage == 'services.php') { echo 'class="current"'; }?> >Services</a> </li> | 
 
<li><a href="website_design_hertfordshire.php"<?php if ($currentpage == 'website_design_hertfordshire.php') { echo 'class="current"'; }?> >Website Design</a> </li>| 
 
<li><a href="ecommerce_websites_hertfordshire.php" <?php if ($currentpage == 'ecommerce_websites_hertfordshire.php') { echo 'class="current"'; } ?>>Ecommerce Solutions</a></li>|  
 
<li><a href="portfolio.php" <?php if ($currentpage == 'portfolio.php') { echo 'class="current"'; }?>>Portfolio</a></li>|
 
<li><a href="quote.php" <?php if ($currentpage == 'quote.php') { echo 'class="current"'; }?>>Quotation</a></li>| 
</ul>
 
<?php }
else {
?>
<ul>| 
<li><a href="../index.php">Home </a></li>| 
 
<li><a href="../services.php">Services</a> </li> | 
 
<li><a href="../website_design_hertfordshire.php">Website Design</a> </li>| 
 
<li><a href="../ecommerce_websites_hertfordshire.php">Ecommerce Solutions</a></li>|  
 
<li><a href="../portfolio.php">Portfolio</a></li>|
 
<li><a href="../quote.php">Quotation</a></li>| 
</ul>
<?php }
?>
 
 
My thoughts here we that I'd use $currentDirectory = basename(dirname(__FILE__)); to find the name of the current directory and if it was camweb (which is the name of my websites root directory) then it would display the menu with the links as index.php portofolio.php etc and then I put in an else statement to say that if the directory name wasn't camweb then to display the links as ../index.php and ../portfolio.php

I'm a total php newbie and tried this out but sadly this hasn't worked :-( as the dirname basis it on the location of the include file itself rather than the file that the include is inserted in.

Although this example hasn't worked hopefully I've made myself a bit clearer to anyone who may be able to help.

Regards,

Clare.

p.s have I inserted the code in to the forum correctly this time?

Re: Include menu for subdirectory

Posted: Wed Jun 25, 2008 5:43 pm
by JAB Creations
You may want to have a look at this page and compare your file's current path then...
http://us3.php.net/manual/en/reserved.v ... server.php

Re: Include menu for subdirectory

Posted: Wed Jun 25, 2008 11:23 pm
by WebbieDave
Why not use absolute URL's rather than relative ones? So instead of:

Code: Select all

<a href="index.html">
You can do:

Code: Select all

<a href="/index.html">

Re: Include menu for subdirectory

Posted: Thu Jun 26, 2008 4:14 am
by camkorn
Hiya

Thanks for taking the time to reply. I had a read of that page you linked and done a bit more searching and came up with the following solution:

Code: Select all

<?php
$currentDirectory = basename(dirname($_SERVER['SCRIPT_FILENAME'])); 
if ($currentDirectory == 'camweb') { 
$currentpage = basename ($_SERVER ['SCRIPT_NAME']);
?>
This gets the name of the directory the file is located in rather than where the include is located, I then used the if statement to select which menu will be displayed. I guess this method isn't the most ideal way of accomplishing this. My other thought is to somehow count the number of directories the file is located in to then specify which menu is displayed, but perhaps I'm over complicating things a bit!!!