I don't care what language it is, but I'd like this to be done automatically. I have several links on my website that link to the same place regardless. For example, on index.php, I have a home button that links href="index.php" However, when I'm now in downloads/download.php, I need to manually adjust the location to href="../index.php"
Is there a way to do this dynamically? It really messes up considering I'm using the include function for the whole top navigation bar, and I can't really make it dynamic. My ideal languages to accomplish this would be: PHP > JavaScript = jQuery > AJAX
Use relative URLS: Instead of linking to ../index.php, link to: / Other links can be relative too, like: /downloads/download.php
Btw. If using a downloads subdir, why not have an index.php there too? You dont need to link to index-files, just link to theire containing folder.
Instead of using a local directory use a URL. So, you can place http://example.com/index.php
as your href
attribute for all of those.
You can also use the super global $_SERVER
to retrieve the base URL.
function getHomeURL() {
return $_SERVER[ "HTTP_ORIGIN" ];
}
This way you can call the function when you need to place href attributes.
<a href="<?php echo getHomeURL(); ?>">Home</a>
I hope this helped.
Another way to approach this but maybe not the most straight-forward method to keep all of your stuff neatly in one place is via a switch()
you make a single index.php file that looks something like this:
<?php
include 'navbar.php';
$pagelink = $_GET['link'];
switch($pagelink)
{
case 'home';
$link = "home.php";
break;
case 'download';
$link = "download/downloads.php";
break;
case 'about';
$link = "about/about.php";
break;
case 'contact';
$link = "contact/contact_us.php";
break;
case 'this';
$link = "something/else/in/a/different/folder/this.php";
break;
}
include $link;
include 'footer.php';
?>
Then all you have to do is formulate all of your links something like this:
<a href="index.php?link=download" title="Downloads" target="_self">Downloads</a>
and you'l never leave your index page, loading (including) each and every page, wherever it may be located, within the index page.