Well... I'm not sure this method is the most efficient way to do something like this, and this only works one level deep...
Parent
Sub1
Sub2
Sub3
And NOT
Parent
Sub1
SubSub1
SubSub2
Sub2
Etc
Anyway, here's the code (this works, but is still under development. I use this to populate a javascript "dropdown" menu bar)
include ("global.php");
$link = mysql_connect("localhost","user","secret") or die("Fatal Error: Unable to connect to mySQL!");
mysql_select_db("eba") or die("Fatal Error: Unable to open eba database!");
$query = "SELECT LinkID,LinkText,LinkURL,LinkParent,LinkAccessBit FROM navigation WHERE LinkVisible=1 AND LinkMenu=$menu ORDER BY LinkOrder";
$result = mysql_query($query) or die("Fatal Error: mySQL Query Failed");
// Build Menu Arrays
while ($data = mysql_fetch_array($result))
{
$id = $data['LinkID'];
$text = $data['LinkText'];
$url = $data['LinkURL'];
$parent = $data['LinkParent'];
$accessbit = $data['LinkAccessBit'];
if ($bit[$accessbit]==1)
{
// Here is where the arrays are built (Main and Sub Menus)
if ($parent==-1)
{
$main_array[]="\"<center>$text</center>\", \"$url\"";
$sort_array[]=$id;
}
else
{
$sub_array[]="dhtmlMenu.addItem(new NavBarMenuItem(\"$text\", \"$url\"));";
$sub_sort_array[]=$parent;
}
}
}
$ctr=-1;
$ct=0;
foreach($main_array as $entry)
{
$ct++;
}
$bs=((800/$ct)-5);
$barsize="($bs,100)";
foreach($main_array as $entry)
{
$ctr++;
$navigation_array = "$navigation_array
dhtmlMenu = new NavBarMenu$barsize;
dhtmlMenu.addItem(new NavBarMenuItem($entry));";
$ctr2=-1;
$x=$sort_array[$ctr];
foreach($sub_sort_array as $xyz)
{
$ctr2++;
if ($xyz==$x)
{
$navigation_array = "$navigation_array\n$sub_array[$ctr2]";
}
}
$navigation_array = "$navigation_array\nmyNavBar1.addMenu(dhtmlMenu);\n";
}
echo(template("MainIndex"));
Like I said, probably not the most effieient way to do it, but it works!
Also, I know this post is a bit vague at explaining how this works, but take a close look at the 4 arrays and you'll probably get the idea. If not, I'll check back tomorrow and try to help more. 🙂