Jump to content

Using PHP for site navigation to pages in subfolders


djkirstyjay

Recommended Posts

Good evening folks. I could do with a little assistance with some PHP I am attempting. I apologise in advance if it seems basic, but to me it's a little confusing.

 

I have put a page navigation system on my site using a menu.php include with links such as the following :

 

<ul>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?main=homepage">Homepage</a></li>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?main=about">About Us</a></li>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?main=contact">Contact Us</a></li>
</ul>

 

I have included the following code in the main body of index.php :

 

<?php
if(!isset($_GET['main']) or $_GET['main'] == "") {
$loadpage = "homepage.php";
}
elseif (is_file($_GET['main'] . ".php")) {
$loadpage = $_GET['main'] . ".php"; 
}
else { 
$loadpage = "includes/error.php"; 
}
include($loadpage); 
?>

 

All is working fine for any of the pages in the site root, but if I want to link to a page in a sub folder, this is where it all goes horribly wrong and that's what I need to do...

 

So, say that I want to link to my gallery index page which is located in gallery/index.php, how would I do that?

 

I have tried the following, which seemed logical to me :

 

<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?main=gallery/index">Gallery</a></li>

 

but it didn't work... so I could do with help if anyone can point me in the right direction!

 

Thanks in advance :)

I don't think you want to include from a URL.  That forces a network call instead of just a read from the file system.

 

It is more likely that the value of $_GET['main'] is escaped or url encoded in some way.  You need to look at what the value is when you use the subdirectory.  If gpc_magic_quotes is on, you will have to stripslashes() on that value before trying to use it.

Thanks to all for the help, as I did as suggested and found out the result of $loadpage only to make myself more confused, as it seemed to be fine... then I discovered that the page I was linking to didn't actually exist yet, hence the error.

 

Now... how silly do I feel after spending hours wondering why it didn't work! Doh!  ::)  :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.