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 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Automatic Menu Navigation

    Hi, I am trying to create a top-level navigation system that "dynamically" creates itself based on the file path.

    For example:

    mydomain.org/learn/calculus1/chapter3/lesson8/critical-points.php

    Should have the menu:
    ============================
    Learn: Calculus1 Chapter3 Lesson8
    ============================

    Each word is hoverable and will reveal a drop down menu to go to other subjects , chapters, and lessons.

    I want to write one menu file, where the hoverable link is determined by a php function which uses the file path. (as opposed to writing a unique menu for every page.)

    My only idea so far is to check to see if each word is in the url, then return the subtring if it is. Is there a more efficient way to do this as opposed to writing out each case? I.E. check lesson1 check lesson2 check lesson3...sounds like I should use a loop but other ideas/comments would be really helpful as I am new to coding and php. Thanks!

    PHP Code:
    function getMainDirectory()
    {    
        
    $path getAddress(); //getAdress() gets the full url

        //**Check to see if Learn, About, or Donate are in the url, if they are set them to the mainDirectory, otherwise return Home
        
    if(strpos($path"about") !==FALSE){
            
    $mainDirectory substr($path,strpos(getAddress(), 5));
        }
        
        elseif(
    strpos($path"learn") !==FALSE){
            
    $mainDirectory substr($path,strpos(getAddress(), 5));
        }
        elseif(
    strpos($path"donate") !==FALSE){
            
    $mainDirectory substr($path,strpos(getAddress(), 6));
        }
        else{
            
    $mainDirectory "Home";
        }
        return 
    $mainDirectory;

    Last edited by rootmath; 12-03-2010 at 05:12 PM. Reason: needed more accurate title

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Seems reasonable to me, though I wouldn't do it quite like this. From years of SEO experience, you shouldn't have multiple URLs with the same content, you should be pretty specific about what you're displaying. If you want multiple URLs to link to the same page, redirect them to it using header('Location: ...', 301);

    PHP Code:
    $uri parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    if (!
    $uri) {
      return 
    'Home';
    }

    // If it has a / (after the first one), split it up, otherwise copy the path directly
    if (strpos($uri'/'1) === false) {
      
    $uri_first $substr($uri1);
    } else {
      list(
    $uri_first, ) = explode('/'$uri);
    }

    switch(
    $uri_first) {
      case 
    'about':
        return 
    'about';
        break; 
    // Break isn't needed because of the return, but it's supposedly good practice to include it anyway...
      
    case 'learn':
        return 
    'learn';
        break;
      default:
        return 
    'home';

    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • Users who have thanked Lamped for this post:

    rootmath (12-03-2010)

  • #3
    New Coder
    Join Date
    Oct 2010
    Posts
    29
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Lamped View Post
    Seems reasonable to me, though I wouldn't do it quite like this. From years of SEO experience, you shouldn't have multiple URLs with the same content, you should be pretty specific about what you're displaying. If you want multiple URLs to link to the same page, redirect them to it using header('Location: ...', 301);
    Hey thanks for the reply, although I don't think I was very clear with my question. Its not that I want several url's with the same content, its that I want the Navigation Menu to "determine itself" based on the url. Here are more examples:

    1. mydomain.org/index.php
    =====================
    Home
    =====================

    2. mydomain.org/learn/calculus1/welcome.php
    =====================
    Learn Calculus 1
    =====================


    3.Hovering over "Learn" would look like this:

    mydomain.org/learn/calculus1/chapter3/lesson1/critical-points.php
    =============================
    Learn Calculus 1 Chapter 3 Lesson 1
    -----=========================
    Home
    -----
    Learn
    -----
    About
    ------
    Donate



    In this way the navigation menu doubles as a status bar. I got it working great with an adaptation of your code. In particular I wasn't familiar with the parse_url() function

    PHP Code:
    function get_directory($directory_number){

        
    $uri parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); //Get the directories after the domain. Thanks to Lamped at CodingForms.com
        
    $directory explode('/'$uri);     //send the directories to an array
        
    $num_of_elements count($directory);
            if (!
    $uri) { //if there are no directories we are at home
                  
    return 'Home';
            }elseif(
    $directory_number $num_of_elements//make sure that the directory is valid
        
    {
            return 
    $directory[$directory_number];            
        }
        else{
            return 
    "";
        }
        

    Last edited by rootmath; 12-03-2010 at 06:49 PM.


  •  

    Posting Permissions

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