Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts

    PHP Menu Generator Not Ignoring Subdirectories

    Nevermind, I figured it out!

    -------------------------------------------------------------------
    I have built my first PHP menu generator. This is the script I'm using to generate a menu system:
    PHP Code:
    <?php
    $path 
    $_SERVER['DOCUMENT_ROOT']."/php/";
    $dir_handle = @opendir($path) or die("Unable to open $path");
    while (
    $file readdir($dir_handle)) 
    {
       if(
    $file!="." && $file!=".." && $file!="index.php" && !strpos($file".html") && !is_dir($file)){
          
    $file_copy $file;
          
    $file_page $file;
          
    $file_page str_replace(".php","",$file_page);
          
    $file_page str_replace(".html","",$file_page);
          
    $file_page str_replace(".css","",$file_page);
          
    $file_copy str_replace("_"," ",$file_copy);
          
    $file_copy str_replace(".php","",$file_copy);
          
    $file_copy str_replace(".html","",$file_copy);
          
    $file_copy str_replace(".css","",$file_copy);
          
    $file_copy str_replace("php","PHP",$file_copy);
          
    $file_copy str_replace("html","HTML",$file_copy);
          
    $file_copy str_replace("css","CSS",$file_copy);
          
    $file_copy ucwords($file_copy);
          echo 
    "          <li><a href=\"/php/?page=$file_page\"";

    if(
    strlen($file_copy)>10 && strlen($file_copy)<=18){
    echo 
    " style=\"width:11.5em;\"";
    }
    else if(
    strlen($file_copy)>18 && strlen($file_copy)<=26){
    echo 
    " style=\"width:13em;\"";
    }
    else if(
    strlen($file_copy)>26 && strlen($file_copy)<=40){
    echo 
    " style=\"width:20em;\"";
    }
    echo 
    ">$file_copy</a></li>\n";
       }
    }
    closedir($dir_handle);
    ?>
    I know the code isn't optimized and I'm using some extra instances of variables when I don't need to, but for now I'm more concerned with the following:

    This code is in a file called "menu.php" which is located in the root directory. As you can see, I am filtering out ".", "..", the index page, and any ".html" files located in the directories. I'm also using !is_dir($file) in the if statement to prevent subdirectories from being returned as menu <li> items.

    The code above works perfectly when the menu.php file is included in the "php" directory, however when the same include is made to a file in any other directory the menu lists the subdirectories under the php folder. The ".", "..", the index page, and ".html" files are still properly ignored but for some reason the subdirectories pop back in.

    I think this is the only really relevant code:
    PHP Code:
    <?php
    $path 
    $_SERVER['DOCUMENT_ROOT']."/php/";
    $dir_handle = @opendir($path) or die("Unable to open $path");
    while (
    $file readdir($dir_handle)) 
    {
       if(
    $file!="." && $file!=".." && $file!="index.php" && !strpos($file".html") && !is_dir($file)){

    ...

    }
    And I can't find anything in there to make sense of this problem.

    As I understand it this should just be loading the same exact menu only in a different page but somehow it seems to be tied to the directory that houses the page that is calling up the menu (if that makes sense). Can anyone tell me why this is behaving like this and how I can stop it?

    This is the page I am using it on. Let me know if I need to provide more info.

    -------------------------------------------------------------------
    Last edited by Rowsdower!; 05-06-2009 at 09:05 PM. Reason: I figured it out all by my lonesome!
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •