In this, my first post, I'll present a working but perhaps not so smart way of generating a dropdown javascript menu using mysql. The menu is written by Michael Leigeber, is very lightweight and can be found over at his site: http://www.leigeber.com/2008/11/drop-down-menu/
I'm sure my script can be improved and that's why I'm posting it here. I think that feedback from more experienced programmers is the best way to acquire knowledge so here goes.
The database
Code: Select all
+---------------+
| Tables_in_cea |
+---------------+
| eng_menu |
| eng_sub_cea |
| eng_sub_join |
| eng_sub_who |
+---------------+
Code: Select all
+----+---------------+-------------+--------------+
| id | name | link | expand_table |
+----+---------------+-------------+--------------+
| 1 | Who are we? | who.php | eng_sub_who |
| 2 | Stake Holders | stake.php | |
| 3 | Join us! | join.php | eng_sub_join |
| 4 | Events | events.php | |
| 5 | Contact | contact.php | eng_sub_cea |
+----+---------------+-------------+--------------+
Code: Select all
<?php
function display_menu()
{
$sql = "SELECT * FROM eng_menu";
$result = mysql_query($sql);
$menu .= "<ul class=\"menu\" id=\"menu\">\r";
while ($row = mysql_fetch_assoc($result))
{
if (empty($row["expand_table"])) // Does not expand first level
{
$menu .= "<li><a href=\"$row[link]\" class=\"menulink\">$row[name]</a></li>\r";
}
else // Does expand first level
{
$menu .= "<li><a href=\"$row[link]\" class=\"menulink\">$row[name]</a>\r<ul>\r";
$sql2 = "SELECT * FROM ".$row["expand_table"];
$result2 = mysql_query($sql2);
while ($row2 = mysql_fetch_assoc($result2))
{
if (empty($row2["expand_table"])) // Does not expand second level
{
$menu .= "<li><a href=\"$row2[link]\">$row2[name]</a></li>\r";
}
else // Does expand second level
{
$menu .= "<li><a href=\"$row2[link]\" class=\"sub\">$row2[name]</a>\r<ul>\r";
$sql3 = "SELECT * FROM ".$row2["expand_table"];
$result3 = mysql_query($sql3);
$i = 1;
while ($row3 = mysql_fetch_assoc($result3))
{
if ($i == 1)
{
$menu .= "<li class=\"topline\"><a href=\"$row3[link]\">$row3[name]</a></li>\r";
}
else
{
$menu .= "<li><a href=\"$row3[link]\">$row3[name]</a></li>\r";
}
$i++;
}
$menu .= "</ul>\r</li>\r";
}
}
$menu .= "</ul>\r</li>\r";
}
}
mysql_free_result($result);
mysql_free_result($result2);
mysql_free_result($result3);
mysql_close();
echo $menu;
}
display_menu();
?>