DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   php (http://www.daniweb.com/code/php.html)
-   -   Extremely Simple Menu Generator (http://www.daniweb.com/code/snippet761.html)

ShawnCplus php syntax
Sep 11th, 2007
This is about as simple as it gets with menu generators. Everything is explained in the code. Note that the menu is simply an unordered list so to pretty it up it really relies on the CSS.

If you don't want to write any CSS of your own here is a sample stylesheet.

#Menu 
{
        position:absolute;
        width:95%;
        height:44px;
        z-index:3;
        left: 4px;
        top: 175px;
} .tabbedmenu
{
    padding: 3px 0;
    margin-left: 0;               
    font: bold 12px Verdana;
    border-bottom: 2px groove #F16C0A;
    list-style-type: none;
    text-align: left;
}
.tabbedmenu ul
{
    margin: 0;
    padding: 0;
    float: left;
}
.tabbedmenu li
{
    display: inline;
    position: relative;
    margin: 0;
}
.tabbedmenu li li{display: block;}
.tabbed menu li li:hover{display: inline;}

.tabbedmenu li a
{
    text-decoration: none;
    padding: 3px 7px;
    margin-right: 3px;
    border: 3px double gray;
    border-bottom: none;
    background-color: #000000;
    color: #FFFFFF;
}   
.tabbedmenu li a:hover
{
    background-color: #F16C0A;
    color: black;
}
.tabbedmenu li a:active {color: #FFFFFF;}
.tabbedmenu li a.selected
{
    position: relative;
    top: 1px;
    padding-top: 4px;
    background-color: #F16C0A;
    color: white;
}

  1. <?php
  2. /*
  3.  * @brief print the menu to the page
  4.  * @param string selected What is the current page?
  5.  */
  6. function writeMenu ($selected="index")
  7. {
  8. /* the only tedious part is that a boolean variable has to be made
  9.   * for each new page and a case put in the switch */
  10. $index =
  11. $staff =
  12. $contact =
  13. $links = false;
  14. $sel = 'class="selected"'; //change the class for selected page here
  15. switch($selected)
  16. {
  17. case 'staff':
  18. $staff = true;
  19. break;
  20. case 'links':
  21. $links = true;
  22. break;
  23. default: //fallthrough
  24. case 'index':
  25. $index = true;
  26. break;
  27. }
  28. // add pages in this array
  29. $pages = array("index", "staff", "links");
  30.  
  31. //Change the div ID and the list class here
  32. echo '<div id="Menu">'."\n\t".'<ul class="tabbedmenu">';
  33.  
  34. /*
  35. * This loop will generate a link on the menu.
  36. * Page name is automatically generated by the link.
  37. * IE: pages.php will show up as Pages on the menu
  38. * (can be overridden as shown below by explicitly
  39. * setting the value).
  40. */
  41.  
  42. for($i=0;$i<count($pages);$i++)
  43. {
  44. $html = "\n\t\t".'<li><a href="'.$pages[$i].'.php" ';
  45. $html .= ($$pages[$i]==true)?$sel:"";
  46. $pages[0]= "home"; //pagename override
  47. $html .='>'.ucfirst($pages[$i]).'</a></li>';
  48. echo $html;
  49. }
  50. $html .= '</ul>'."\n".'</div>'."\n";
  51. echo $html;
  52. }
  53. ?>