Results 1 to 8 of 8

Thread: Include Menu folders

  1. #1
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Include Menu folders

    Hey guys so i have a small problem i have a website with folder as well and i use for the menu of each page a include file.
    In the for every page in the home folder everything ok the links work great but i have a folder in the site as well for example:
    website-home-folder/article-folder
    and when i use include menu on the "article-folder" the link not working because to have the link working i need to set up on the file to go a folder back like this "../" so my solution for now was to set up to include pages one with "../" on every link and one without that.
    And now i want to create another folder inside a folder, for example:
    website-home-folder/article-folder/new-folder
    so i will need to create another include page with "../../" which is starting to be a lot of files wanted to know if there is some kind of why to get it to identity if the folder is the main folder and if it is not it will go "../" if it will still not be the main folder it will go again "../"
    is there some kind of function in php that does that?

  2. #2
    Join Date
    May 2010
    Location
    Sacramento, CA
    Posts
    91
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default

    I ran into this problem a few months back, here was my solution that worked pretty well; I used
    PHP Code:
    file_exists(); 
    http://php.net/manual/en/function.file-exists.php
    http://php.net/manual/en/function.dirname.php

    file_exists() will check if the file...well, EXISTS!
    Here's how I used it:

    PHP Code:
    if(file_exists('menu.php')){ require_once('menu.php');}
    elseif(
    file_exists('../menu.php')){ require_once('../menu.php');}
    elseif(
    file_exists('../../menu.php')){ require_once('../../menu.php');}
    elseif(
    file_exists('../../../menu.php')){ require_once('../../../menu.php');} 
    It will find the directory the file your looking for is in.
    Last edited by crobinson42; 02-27-2012 at 01:50 AM.

  3. The Following User Says Thank You to crobinson42 For This Useful Post:

    Oalddesign (02-27-2012)

  4. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    If you want to include a file but not make it necessary, require() is the wrong function. Use include() or include_once(). They do exactly the same thing, except that require will throw a fatal error (and end execution) if the file is not found. Include will just give a warning.

    And if you want to hide that warning:
    @include_once('....'); (error suppression with @)

    Generally what crobinson42 posted is good, though, because checking if it exists, then including it is a reasonable way to go. But if you definitely don't want to worry about the file not existing, I suppose you might as well use error suppression-- let code.


    ...However, there is probably a better way to go about solving this problem.

    1) Create one central configuration file. Name that "configuration.php" or "main.php" or whatever you want. Put it anywhere you'd like.
    2) In each page, type the correct relative location, such as "../main.php", "main.php" or "mydir/main.php". (This is not much work if you're writing each file by hand.*)
    3) Now, within main.php, you can use the special constant __FILE__ and make everything relative to that. For example: include(dirname(__FILE__').'/other.php'); will include the file named "other.php" that is in the SAME folder as main.php. __FILE__ gives the true filename (on the server) of the currently active file, even when it's included into another file-- note that this is very unusual (but useful) behavior in PHP.

    I use that all the time when I have a central PHP file that controls my site (such as including functions.php and database.php etc.)


    *If you're actually automatically generating PHP files, then I suppose this might be a little difficult. But in doing so, you will know the path and could figure out the relative path to main.php, so you could automatically put the right path in there rather than guessing a few times.



    The only other option here is to use absolute paths. That might be easiest, but often they're ugly and a little hard to work with. You'll have to check what the absolute path is (based on the root directory of your hosting account, as seen by PHP), but here's a quick introduction to the method:
    1) Use echo __FILE__; to find out the current file's absolute location. You'll get something like /home/mystuff/web/http/mydir/main.php and from there you can determine the absolute path to any file on your system.
    2) Now you can use that base and connect it to any file you'd like. Then use that method to include the absolute file (NOT relative file) from any page:
    include('/home/mystuff/web/http/mydir/main.php'); (always include main.php, no matter where it is called from)
    Note the initial / for an absolute location.
    Last edited by djr33; 02-27-2012 at 04:16 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    Oalddesign (02-27-2012)

  6. #4
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thank you very much for both i was able to create a simple function from what crobinson42 and make it work,
    djr33 thanks a lot for you long replay i have tried your advice as well but for some reason i wasn't able to make it work i probably didn't got it right what you wanted.
    Thanks again for both for your for your great help.

  7. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Note that the end of my post (about absolute paths) is perhaps the "easiest" (it's direct), but it can be annoying to deal with those lines paths. And it's especially hard when you might change servers or move your files around. But it'll definitely work.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #6
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the note by the way for those who have the same problem this is what i end up creating from crobinson42 code
    I have placed a file that called "home-folder" inside the main folder where all the files are


    <?php
    function menu_link() {
    if (file_exists('home-folder')) {echo "";}
    elseif (file_exists('../home-folder')) {echo "../";}
    elseif (file_exists('../../home-folder')) {echo "../../";}
    }
    ?>

    <a href="<?php menu_link() ?>index.php"> Home </a>

    And the <?php menu_link() ?> echoing the amount of "../" i need for each file seems like it working nicely.

  9. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You want this for a link? You're not using include()?

    If you don't need this in the PHP system (you don't need the full system path), then this is VERY easy and you can completely avoid anything complex:

    Solution 1: Use the full URL including domain name. yoursite.com/folder/file.htm/php.

    Solution 2: Use the short version of that, which is /folder/file.htm/php.
    This "/" at the beginning works as I explained in the previous post, but since it's on your HTTP website, it just jumps back to the domain name.

    So a link of / from yoursite.com/folder/folder/file.htm would always take you to your home page.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #8
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    I do use include the content that you see in there is the menu.php all the files in the folders including that file.
    Thanks i know about "/" and about the fact that i can put in there the domain name but i edit the site a lot on my computer using wamp so if i will change it to the domain or to "/" i won't be able to use the site menu on wamp.
    and that code seems to be helpful "home-folder" is a file i have created in the main folder so the code echo the right amount of "../" i need for the link to work even in the link is inside a folder. So this is the solution i wanted thanks for trying to help again.

Bookmarks

Posting Permissions

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