Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hey,
I'm doing a webpage and have probelms with the menu (which I'm making with PHP).
To check if I want the submenu to be visible I check the current location and then use a if-statement:
// define our current location
$current_url = $_SERVER['PHP_SELF'];if(($current_url == "/second/index.php") or ($current_url == "/second/file.php") or ($current_url == "/second/form.php") or ($current_url == "/second/this.php")){
...Then show the submenu.
Now I'm looking for a way to only check what the current folder is, so that I don't have to check all the files separetly. Something like this:
if($current_folder == "/second/"){
...Then show the submenu }Can you guys help me?
"Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies." - Linus Torvalds

$current_folder = basename(dirname(__FILE__));
This will set the value to the folder in which the currently running file resides (w/o the slashes).
Using your example above the code would be written:
if($current_folder == "second"){
...Then show the submenu }Michael J

Thanks for the help but, it doesn't work.
Am I supposed to leave this as it is:
$current_folder = basename(dirname(__FILE__));
Or modify anything in it?
Peace, rade
"Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies." - Linus Torvalds

When you say "It doesn't work" please be specific. Are you getting errors? If so, what are they? Have you done ANY error checking?
If I have a php file with the code below, I can put it in any folder and when I run that page it will display the name of the folder that the file is in.
<?php
$current_folder = basename(dirname(__FILE__));
echo $current_folder;
?>Michael J

Oh, sorry. I was in a hurry last time I wrote.
To be specific: I have the whole navigation code in leftmenu.php, which I then have included with php include(); in all the sites.
Now I came to think of, does the server read the PHP in leftmenu.php before or after it has included it in eg main.php? Because if it reads it and converts it to html code before it's included in main.php, that's why it didn't work.
I'm at the office now but will test more when I come home. Catch you later!
"Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies." - Linus Torvalds

Sorry, I don't follow you. When you "include" a file it is processed in-line. In other words it's the same as if you had copied and pasted the code from the include file at the same position as you put the include command.
Try echoing the value of $current_folder to the page right after you create it and see if it is what you are expecting. You should also echo the value just before you use it in your conditional statement. You might have a problem with variable scope.
Forgive me if I am pointing out the obvious. But, when developing you need to do these simple debugging processes to ensure your values are what you are expecting whenever your conditionals (IF statements) are not functioning properly.
Michael J

It's okay, I'm very glad you are willing to help. Now:
This is how the files are organised:
------
root
/main
- index.php
/second
- index.php
- file.php
- form.php
/system
- leftmenu.php
------leftmenu.php contains the whole navigation code.
all the other files are sites which have this code there where the navigation is supposed to be:<?php
include ("../system/leftmenu.php");
?>This is my goal: When the user is browsing any file in the folder named "second" a submenu would appear. This is what I need the if-statement for.
So if you are browsing main/index.php the navigation would show links to:
- main/index.php
- second/index.phpAnd if you are browsing any file in "second", eg second/file.php the navigation would show links to:
- main/index.php
- second/index.php
-- second/file.php
-- second/form.phpI tested having this in the leftmenu.php and was browsing main/index.php:
$current_folder = basename(dirname(__FILE__));
print($current_folder);And it printed "system". Because the leftmenu.php is in the "sytem" folder.
BUT, when I had this code in the leftmenu.php and browsed main/index.php:$current_url = $_SERVER['PHP_SELF'];
print($current_url);It printed "/main/index.php".
And having this code in main/index.php itself:
$current_folder = basename(dirname(__FILE__));
print($current_folder);Printed "main".
So what would solve my problem (I think), is by somehow getting the current folder by using this method:
$current_url = $_SERVER['PHP_SELF'];
What do you think?
/Rade
"Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies." - Linus Torvalds

I did it!
This is how:
$current_url = $_SERVER['PHP_SELF'];
$pieces = explode("/", $current_url);
$current_folder = $pieces[1];So first I took the $current_url, which would print "second/index.php" (when browsing second/index.php).
Then I split $current_url up with the explode() function into the array $pieces, so every part would be separated by a "/". This gives us the parts:
$pieces[1] = "second"
$pieces[2] = "index.php"Then I put the $current_folder to the same as $pieces[1] (which is equal to the current folder).
And finally, I used it in my if-statement:
if($current_folder == "second"){
...Then show the submenuThank you for your help! And IF you come up with another solution, I'd love to hear that too.
Peaces & Good luck, Rade
"Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies." - Linus Torvalds

OK, you are putting your code into the leftmenu.php file and want it to return the root folder of the file that included leftmenu.php. Here's an easier option:
$current_folder = basename(dirname($_SERVER['PHP_SELF']));
Michael J

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |