0

I am using following code to generate menus

<?php 
$menu = array(); 
$menu['home'] = 'Home'; 
$menu['mypage'] = 'My Page'; 
//Add in the format of: $menu['page name'] = 'Page Title'; 
$title='Home'; //Default title 
function generateMenu()    { 
    global $menu,$default,$title; 
    echo '    <ul>'; 
    $p = isset($_GET['p']) ? $_GET['p'] : $default; 
    foreach ($menu as $link=>$item)    { 
        $class=''; 
        if ($link==$p)    { 
            $class=' class="selected"'; 
            $title=$item; 
        } 
        echo '<li><a href="?p='.$link.'"'.$class.'>'.$item.'</a></li>'; 
    } 
    echo '</ul>'; 
} 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>My PHP Template - <?php echo $title; ?></title> 
</head> 

<body> 
<?php 
generateMenu(); 
$default = 'home'; //Whatever default page you want to display if the file doesn't exist or you've just arrived to the home page. 
$page = isset($_GET['p']) ? $_GET['p'] : $default; //Checks if ?p is set, and puts the page in and if not, it goes to the default page. 
if (!file_exists('inc/'.$page.'.php'))    { //Checks if the file doesn't exist 
    $page = $default; //If it doesn't, it'll revert back to the default page 
    //NOTE: Alternatively, you can make up a 404 page, and replace $default with whatever the page name is. Make sure it's still in the inc/ directory. 
} 
include('inc/'.$page.'.php'); //And now it's on your page! 
?> 
</body> 
</html>

But I have 6 menus and first three menu have sub menus. Above code generates only menus. I want to add sub menu also.
Can anyone tell me HOW TO SOLVE THIS?

2
Contributors
3
Replies
8
Views
6 Years
Discussion Span
Last Post by vizz
0

Well, there is so much information its hard to answer this one without knowing how you want your menus to display - especially with submenus, drops downs, etc you have many options, so its hard to give you an exact answer.

I am confident I can get you started though, you can tweak the code as needed. I changed a lot but I think I still covered all the functionality.

Feel free to ask question if you have any trouble.

<?php
// First, let's figure out your page
$page = $_GET['p']; // Get page name
if(empty($page)){$page = "home";} // Better to use empty instead of isset
//Check if page exists, otherwise set page to error 404 and set error 404 header:
if(!file_exists("inc/$page.php")){$page = "error404"; header("HTTP/1.0 404 Not Found");}


//Lets decide how the menu array should hold all the options
$menu = array();

$menu['home']['top'] = 'Home'; // ID of 0, top level navigation.
$menu['home']['sub page name'] = 'Sub Page'; // ID of 1, sub nav
$menu['home']['other sub page name'] = 'Other Sub Page'; // ID of 2, sub nav, etc

$menu['mypage']['top'] = 'My Page'; // ID of 0, top level navigation link.
$menu['mypage']['sub page name'] = 'Sub Page'; // ID of 1, sub nav
$menu['mypage']['other sub page name'] = 'Other Sub Page'; // ID of 2, sub nav, etc

function generateTitle(){
global $menu,$page;

foreach($menu as $k => $v){
  foreach ($v as $kk => $vv){
    if(($kk == "top" && $k == $page) || $kk == $page){
      return($vv);
    }
  }
}

}



//generate menu function
function generateMenu() {
global $page,$menu,$default,$title;

echo "<ul>";
foreach($menu as $k => $v){
foreach ($v as $kk => $vv){
if($kk == "top"){
echo "<li><a href=\"?p=$k\"";
if($k == $page){echo " class=\"selected\"";}
echo ">$vv</a></li>";
echo "<ul>";
}else{
echo "<li><a href=\"?p=$kk\"";
if($kk == $page){echo " class=\"selected\"";}
echo ">$vv</a></li>";
}

}
echo "</ul>";
}

echo "</ul>";
}

//Doctype needs to be put on page before ANY white space, so it has to be directly after the closing php ">" symbol, I like to echo it out in the PHP to make sure though:

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">";

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>My PHP Template - <?php echo generateTitle(); ?></title>
</head>

<body>
<?php
generateMenu();
include('inc/'.$page.'.php'); //And now it's on your page!
?>
</body>
</html>
0

It's great work DUDE!!!

Well, there is so much information its hard to answer this one without knowing how you want your menus to display - especially with submenus, drops downs, etc you have many options, so its hard to give you an exact answer.

I am confident I can get you started though, you can tweak the code as needed. I changed a lot but I think I still covered all the functionality.

Feel free to ask question if you have any trouble.

<?php
// First, let's figure out your page
$page = $_GET['p']; // Get page name
if(empty($page)){$page = "home";} // Better to use empty instead of isset
//Check if page exists, otherwise set page to error 404 and set error 404 header:
if(!file_exists("inc/$page.php")){$page = "error404"; header("HTTP/1.0 404 Not Found");}


//Lets decide how the menu array should hold all the options
$menu = array();

$menu['home']['top'] = 'Home'; // ID of 0, top level navigation.
$menu['home']['sub page name'] = 'Sub Page'; // ID of 1, sub nav
$menu['home']['other sub page name'] = 'Other Sub Page'; // ID of 2, sub nav, etc

$menu['mypage']['top'] = 'My Page'; // ID of 0, top level navigation link.
$menu['mypage']['sub page name'] = 'Sub Page'; // ID of 1, sub nav
$menu['mypage']['other sub page name'] = 'Other Sub Page'; // ID of 2, sub nav, etc

function generateTitle(){
global $menu,$page;

foreach($menu as $k => $v){
  foreach ($v as $kk => $vv){
    if(($kk == "top" && $k == $page) || $kk == $page){
      return($vv);
    }
  }
}

}



//generate menu function
function generateMenu() {
global $page,$menu,$default,$title;

echo "<ul>";
foreach($menu as $k => $v){
foreach ($v as $kk => $vv){
if($kk == "top"){
echo "<li><a href=\"?p=$k\"";
if($k == $page){echo " class=\"selected\"";}
echo ">$vv</a></li>";
echo "<ul>";
}else{
echo "<li><a href=\"?p=$kk\"";
if($kk == $page){echo " class=\"selected\"";}
echo ">$vv</a></li>";
}

}
echo "</ul>";
}

echo "</ul>";
}

//Doctype needs to be put on page before ANY white space, so it has to be directly after the closing php ">" symbol, I like to echo it out in the PHP to make sure though:

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">";

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>My PHP Template - <?php echo generateTitle(); ?></title>
</head>

<body>
<?php
generateMenu();
include('inc/'.$page.'.php'); //And now it's on your page!
?>
</body>
</html>
0

My css for dynamic menus

<style media="all" type="text/css"> 
body { background: #0D1114; }
ul,li{ list-style:none;}
ul { border: 1px solid #eee; border-bottom: 0; }
li { background: #fff; border-bottom: 1px solid #eee; }
li li { background: #eee; }
a { padding: 0 10px; font: bold 13px/30px 'Arial', 'sans-serif'; color: #036; text-decoration: none; }
</style>

Please Help me to create Accordion

Well, there is so much information its hard to answer this one without knowing how you want your menus to display - especially with submenus, drops downs, etc you have many options, so its hard to give you an exact answer.

I am confident I can get you started though, you can tweak the code as needed. I changed a lot but I think I still covered all the functionality.

Feel free to ask question if you have any trouble.

<?php
// First, let's figure out your page
$page = $_GET['p']; // Get page name
if(empty($page)){$page = "home";} // Better to use empty instead of isset
//Check if page exists, otherwise set page to error 404 and set error 404 header:
if(!file_exists("inc/$page.php")){$page = "error404"; header("HTTP/1.0 404 Not Found");}


//Lets decide how the menu array should hold all the options
$menu = array();

$menu['home']['top'] = 'Home'; // ID of 0, top level navigation.
$menu['home']['sub page name'] = 'Sub Page'; // ID of 1, sub nav
$menu['home']['other sub page name'] = 'Other Sub Page'; // ID of 2, sub nav, etc

$menu['mypage']['top'] = 'My Page'; // ID of 0, top level navigation link.
$menu['mypage']['sub page name'] = 'Sub Page'; // ID of 1, sub nav
$menu['mypage']['other sub page name'] = 'Other Sub Page'; // ID of 2, sub nav, etc

function generateTitle(){
global $menu,$page;

foreach($menu as $k => $v){
  foreach ($v as $kk => $vv){
    if(($kk == "top" && $k == $page) || $kk == $page){
      return($vv);
    }
  }
}

}



//generate menu function
function generateMenu() {
global $page,$menu,$default,$title;

echo "<ul>";
foreach($menu as $k => $v){
foreach ($v as $kk => $vv){
if($kk == "top"){
echo "<li><a href=\"?p=$k\"";
if($k == $page){echo " class=\"selected\"";}
echo ">$vv</a></li>";
echo "<ul>";
}else{
echo "<li><a href=\"?p=$kk\"";
if($kk == $page){echo " class=\"selected\"";}
echo ">$vv</a></li>";
}

}
echo "</ul>";
}

echo "</ul>";
}

//Doctype needs to be put on page before ANY white space, so it has to be directly after the closing php ">" symbol, I like to echo it out in the PHP to make sure though:

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">";

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>My PHP Template - <?php echo generateTitle(); ?></title>
</head>

<body>
<?php
generateMenu();
include('inc/'.$page.'.php'); //And now it's on your page!
?>
</body>
</html>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.