Forum Moderators: coopster

Message Too Old, No Replies

Creating Relative Links Dynamically

Simple navigation issue

         

brodyh

8:45 pm on Apr 30, 2009 (gmt 0)

10+ Year Member



This problem seems like it would be really easy to fix, but for some reason I am stumped.

I have a website hierarchy similar to this:
/
/folder1
/folder1/folder2

In each folder there is a file that has links to pages within it, so obviously it's easy enough to just name the page in my links and it just works. The same thing happens in folder2.
However, I would like to have pages in folder2 display links from folder1 and links in folder2, but the paths need to be relative to the current page (so it would be in folder2) without referring to folder1 by name (this would make it difficult to rename folder1 in the future, etc.).

I already have a variable called $DirR which refers to the root of the website (/). I COULD use this variable and 'folder1' to it but this would defeat the purpose.

I am new to PHP so any help would be appreciated.

Receptional Andy

8:48 pm on Apr 30, 2009 (gmt 0)



Welcome to WebmasterWorld, brodyh :)

I'm not sure I quite understand, but you can link one directory higher using ../

E.g. <a href="../">Next directory up</a>

brodyh

9:18 pm on Apr 30, 2009 (gmt 0)

10+ Year Member



I guess I do need to explain this better, sorry.. :P

/folder1/folder2/dir.php
<?php

$DirR = "../../"; // Points to server root
$Nav = "";

require_once("../dirMenu.php");
require_once("dirMenu.php");

?>

/folder1/dirMenu.php
<?php

$Nav .= "<div><h1>Folder1</h1><ul><li><a href=\"index\">Main</a></li><li><a href=\"anotherFolder1Page\">Another Folder 1 Page</a></li></ul></div>";

?>

/folder1/folder2/dirMenu.php
<?php

$Nav .= "<div><h1>Folder2</h1><ul><li><a href=\"index\">Main</a></li><li><a href=\"page2\">Page2</a></li></ul></div>";

?>

What I would like to do is have PHP correctly output the following for navigation when visiting a page in /folder1/folder2:

<div>
<h1>Folder1</h1>
<ul>
<li><a href="../index">Main</a></li>
<li><a href="../anotherFolder1Page">Another Folder 1 Page</a></li>
</ul>
<h1>Folder2</h1>
<ul>
<li><a href="index">Main</a></li>
<li><a href="page2">Page 2</a></li>
</ul>

But since the dirMenu.php file in folder1 does not already contain the ../ you see in the desired code, I need to somehow tell PHP how to generate that.

punisa

9:34 pm on Apr 30, 2009 (gmt 0)

10+ Year Member



Hello brodyh, I stared at your problem for some 5 minutes, but it messes with my head : D
Wouldn't it be much nicer that you use root links instead of relative ones?
I see you mention that you need relatives ones, but from experience I remember I had similar situation and in the end I spent a day rewriting it all back to full root links, it just got too complicated.

For your menu file - create another folder in root and call it something like "includes", place a single menu file there and then make all your other pages include from there.

Can it be done like that?

brodyh

9:46 pm on Apr 30, 2009 (gmt 0)

10+ Year Member



Yes.. I could do it that way. I am starting to think I will go back to root links.
I already use some init files to write stuff before and after the actual page content loads, so I think I will just make a file called map and put it somewhere.