...

View Full Version : Automatic Menu Navigation



rootmath
12-03-2010, 02:38 AM
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!


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;
}

Lamped
12-03-2010, 08:04 AM
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);


$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($uri, 1);
} 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';
}

rootmath
12-03-2010, 05:08 PM
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


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 "";
}

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum