PHP Classes

myMenuGen: Generate an HTML menu from MySQL database entries

Recommend this page to a friend!
  Info   Example   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 63%Total: 2,939 All time: 1,272 This week: 488Up
Version License PHP version Categories
mymenugen 1.0GNU General Publi...4.0HTML, Databases
Description 

Author

The class can be used to generate an HTML menu from information stored in a MySQL database tables.

It queries a MySQL database and retrieves entries that are used to define the entries of the menu.

The class generates nested HTML lists combined with CSS to implement drop down menus.

Picture of Peter Barkway
Name: Peter Barkway <contact>
Classes: 3 packages by
Country: United Kingdom

 

Recommendations

What is the best PHP menu dropdown mysql class?
I need to construct a menu dropdown stored in MySQL database

Example

<?php

$dbserver
='localhost';
$dbuser='root';
$dbpwd='';
$dbase='mymenugen';

$link = mysql_connect ($dbserver, $dbuser, $dbpwd) or die ("Could not connect");
mysql_select_db($dbase);


include(
"classes/mymenugen_class.php"); // include myMenuGen class

$myMenu = new myMenuObject;

if(isset(
$_REQUEST['saveNew'])){
 
$myMenu->saveHomePage($_POST['homepage']);

 
$menu_id = array();
  for(
$j = 1; $j <= $_POST['iterations']; $j++) {
   
$menu_id[] = $_POST['menu_id'.$j];
  }

 
$tmpArray = $myMenu->getPages();
 
$myMenu->deleteRemovedMenuItems($menu_id); // Remove any existing items before saving them again
 
for($j = 1; $j <= $_POST['iterations']; $j++) {
   
$myMenu->position = $j;
   
$myMenu->menu_title = htmlentities($_POST['menu_title'.$j], ENT_QUOTES);
   
$myMenu->menu_id = $_POST['menu_id'.$j];
   
$myMenu->parent_id = $_POST['parent_id'.$j];
   
$myMenu->menu_image = $_POST['menu_image'.$j];

    foreach(
$tmpArray as $val) {
     
$myMenu->getPage($val);
      if(
$_POST['page_inp'.$j] == $myMenu->page_link) {
       
$myMenu->page_id = $_POST['page_sel'.$j];
        break;
      } else {
       
$myMenu->page_id = '';
      }
    }

    if(empty(
$myMenu->page_id)) {
     
$myMenu->external_link = $_POST['page_inp'.$j];
    } else {
     
$myMenu->external_link = '';
    }
    if(isset(
$_POST['menu_hr'.$j])) { $myMenu->menu_hr = 'yes'; }
    else {
$myMenu->menu_hr = 'no'; }

    if(
$myMenu->itemExists($myMenu->menu_id) && !empty($myMenu->menu_id)) {
     
$myMenu->updateMenuItem($myMenu->menu_id);
    } else {
     
$myMenu->saveMenuItem();
    }
  }
}

if(
count($myMenu->getMenuItemsID()) > 0) { $myMenu->new_record = false; }

$tmpString = '<script type="text/javascript">
                //<![CDATA[
                <!--
                // Find the homepage
                var S_homepage = "";
                var S_homepage_link = "";'
;

// Collect the homepage identity
$homepage = $myMenu->getHomePage();
$tmpString .= 'S_homepage = "'.$myMenu->page_id.'";'."\n";
$tmpString .= 'S_homepage_link = "'.$myMenu->page_link.'";'."\n";
$tmpString .= 'S_homepage_desc = "'.$myMenu->page_description.'";'."\n";

// Create array pagesList
$tmpString .= ' array_count = 0;
                pagesList = new Array();
                var myHash = {id:\'\', title:\'\'};
                pagesList[array_count++] = myHash;'
;

// Collect all the pages and put into an array
$tmpArray = $myMenu->getPages();
foreach(
$tmpArray as $val) {
 
$myMenu->getPage($val);
 
$tmpString .= 'var myHash = {id:\''.$myMenu->page_id.'\', title:\''.$myMenu->page_link.'\', description:\''.$myMenu->page_description.'\'};
                 pagesList[array_count++] = myHash;'
;
}

// Create array menuList
$tmpString .= ' array_count = 0;
                menuList = new Array();
                var myHash = {id:\'\', title:\'\'};
                menuList[array_count++] = myHash;'
;

// Collect all the menu items and put into an array
$tmpArray = $myMenu->getMenuItemsOrdered();
foreach(
$tmpArray as $val) {
 
$myMenu->getMenuItem($val);
 
$tmpString .= 'var myHash = {id:\''.$val.'\', title:\''.$myMenu->menu_title.'\'};
                 menuList[array_count++] = myHash;'
;
}

// Lookup to generate possible homepage select list and set the current homepage
$tmpString .= ' Array.prototype.lookupPage = function (id) {
                  identity=document.getElementById(id);
                  identity.options.length=this.length;
                  var i;
                  for (i=0; i < this.length; i++) {
                    identity.options[i].value = this[i][\'id\'];
                    identity.options[i].text = this[i][\'title\'];
                    if(this[i][\'id\'] == '
.$homepage.')
                      identity.options[i].selected = true;
                  }
                }

                Array.prototype.lookupArray = function (id) {
                  identity=document.getElementById(id);
                  identity.options.length=this.length;
                  var i;
                  for (i=0; i < this.length; i++) {
                    identity.options[i].value = this[i];
                    identity.options[i].text = this[i];
                    switch(id) {
                      case "homepage":
                        if(this[i] == S_homepage) identity.options[i].selected = true;
                        break;

                    }
                  }
                }

                function checkPage(id, titleid) {
                  identity = document.getElementById(id);
                  target = document.getElementById(titleid);
                  user_input = identity.options[identity.selectedIndex].value;
                  if(user_input > 0) {
                    for (i=0; i < pagesList.length; i++) {
                      if(pagesList[i][\'id\'] == user_input) {
                        target.value = pagesList[i][\'description\'];
                      }
                    }
                  } else {
                    target.value = \'\';
                  }
                }

                onload=function() {
                  checkPage(\'homepage\', \'homepageDesc\');
                }

                //-->
                //]]>
              </script>

              <form name="form1" method="post" action="'
.$_SERVER['PHP_SELF'].'">
                <div id="searchLayout">
                  <table cellspacing="0">
                    <tr>
                      <td width="10%"><b>Homepage</b></td>
                      <td width="20%">
                        <select tabindex="1" id="homepage" name="homepage" onchange="checkPage(\'homepage\', \'homepageDesc\')" style="width: 200px;"><option></option></select>
                        <script type="text/javascript">
                          //<![CDATA[
                          pagesList.lookupPage(\'homepage\');
                          //]]>
                        </script>
                      </td>
                      <td align="right"><input id="homepageDesc" name="homepageDesc" type="text" size="60" value="" readonly="readonly"></td>
                    </tr>
                  </table>
                </div>

                <div id="searchLayout">
                  <table id="tblMenus">
                    <thead>
                      <tr class="noPrint" >
                        <td colspan="12"><h4>Menu Items</h4></td>
                      </tr>
                      <tr id="tblMenusHdr">
                        <td width="4%"><b>Ord</b></td>
                        <td width="26%"><b>Menu Title</b></td>
                        <td width="23%"><b>Page Link</b></td>
                        <td width="23%"><b>Item Parent</b></td>
                        <td width="20%"><b>Item Image</b></td>
                        <td width="6%" align="center"><b>Break After</b></td>
                        <td width="8%" colspan="5" align="right">
                          <a href="#" onclick="insertRowToTableEnd();" title="Add new item">
                            <img src="'
.$myMenu->rootDir.'images/mymenu/addline.png" class="noPrint" alt="Add new line at the end of the list" height="24px" width="24px" />
                          </a>
                        </td>
                      </tr>
                    </thead>
                    <tbody></tbody>
                  </table>
                  <p>
                    <br />
                    <div style="float: left;"><input type="submit" id="cancel" name="cancel" class="noPrint" value="Cancel" /></div>
                    <div style="float: right;"><input type="submit" id="saveNew" name="saveNew" class="noPrint" value="Submit" /></div>
                    <input type="hidden" name="iterations" id="iterations" value="0" />
                  </p>
                </div>
              </form>

              <script src="classes/mymenugen.js" type="text/javascript"></script>
              <script language="javascript" type="text/javascript">
                //<![CDATA[
                <!--
                '
;
if(
$myMenu->new_record) {
 
$tmpString .= 'fillInRows(1, pagesList, menuList);';
} else {
 
$tmpString .= 'fillInRows(0, null);';
 
$tmpArray = $myMenu->getMenuItemsOrdered();
  foreach(
$tmpArray as $val) {
   
$myMenu->getMenuItem($val);
   
$tmpString .= 'addRowToTable(null,"'.addslashes(html_entity_decode($myMenu->menu_title, ENT_QUOTES)).'", pagesList, menuList, "'.$myMenu->page_link.'", "'.$myMenu->menu_image.'", "'.$myMenu->menu_hr.'", "'.$myMenu->parent_id.'", "'.$myMenu->page_id.'", "'.$val.'");';
  }
}
$tmpString .= ' //-->
                //]]>
              </script>'
;


echo
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
        <head>
          <title>myMenuGen</title>
          <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
          <style type="text/css" title="currentStyle" media="screen">
            @import url(style/style.css);
            @import url(style/menu.css);
          </style>
          <!--[if IE 7]>
          <style type="text/css" media="screen, tv, projection">
          /*<![CDATA[*/
          .menu li { height: 1%; }
          /*]]>*/
          </style>
          <![endif]-->

          <!--[if lte IE 6]>
          <style type="text/css" media="screen, tv, projection">
            @import url(style/menu4ie.css);
          </style>
          <script src="classes/adxmenu.js" type="text/javascript"></script>
          <![endif]-->
        </head>
        <body>'
;

echo
$myMenu->getVersion();
echo
'<h2>Example Menu</h2>';
echo
'<div id="bcsMenu">';
echo
'<ul class="adxm menu">';
echo
$myMenu->generateMenuStructure();
echo
'</ul>';
echo
'</div>';


echo
'<h2>Menu Generation Form</h2>';
echo
$tmpString;
echo
' </body>
      </html>'
;


?>


Screenshots (1)  
  • screenshot.gif
  Files folder image Files (21)  
File Role Description
Files folder imageclasses (3 files)
Files folder imagedocs (3 files)
Files folder imageimages (1 file, 1 directory)
Files folder imagestyle (3 files, 1 directory)
Plain text file example.php Example Example script
Plain text file test.sql Data Example mysql tables

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:2,939
This week:0
All time:1,272
This week:488Up
 User Ratings  
 
 All time
Utility:83%StarStarStarStarStar
Consistency:76%StarStarStarStar
Documentation:69%StarStarStarStar
Examples:73%StarStarStarStar
Tests:-
Videos:-
Overall:63%StarStarStarStar
Rank:769