View Full Version : Dynamic menu item creation for "Tigra" (DHTML) menu. HELP!

02-10-2006, 03:43 PM
I have a freeware product called "Tigra menu" in use as javascript/DHTML dropdown menu engine. Tigra Menu homepage (http://www.softcomplex.com/products/tigra_menu/)

The menu items will be populated from the database (MySQL), and the table structure I use, is so called "Modified Preorder Tree Traversal", more info here (http://www.sitepoint.com/article/hierarchical-data-database/2)

And the PHP code I've made for generating javascript menu item file (more info here) (http://www.softcomplex.com/products/tigra_menu/docs/#items) is:


header("content-type: text/plain");
mysql_connect (MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD) or die("Database error 1");
mysql_select_db (MYSQL_DATABASE) or die("Database error 2");

echo 'var MENU_ITEMS = ['."\n";

$right = array();

$sql = "
SELECT `id`, `menutext`, `lft`, `rgt`, `parent`, `active`, `hide_in_menus`
FROM `structure`
ORDER BY `lft` ASC";

$res = mysql_query($sql);
$rows = mysql_num_rows($res);

while ($row = mysql_fetch_array($res)) {
if (count($right) > 0) {
while ($right[count($right) - 1] < $row[rgt]) {

if ($row[parent] != 0 && $row[active] == 1 && $row[hide_in_menus] != 1) {
$url = 'main.php?page='.$row[id];

if (count_childs($row[lft], $row[rgt]) >= 1) {
// Has one or more child nodes
$link = "['".($row['menutext'])."', '".$url."', null,\n";
$open = 1;
} else {
// Zero child nodes
$link = "['".($row['menutext'])."', '".$url."'],\n";

if (count($right) < $prev_count) {
$open = 0;
echo str_repeat(str_repeat(' ', (count($right) * 4))."],\n\n", ($prev_count - count($right)));

echo str_repeat(' ', (count($right) * 4)).$link;
$prev_count = count($right);
$right[] = $row[rgt];

if ($i >= $rows) {
if ($open == 1) {
echo "],\n";
echo "

// Counts the amount of the child nodes
function count_childs($left, $right) {
return (($right - $left - 1) / 2);


And what is the problem?
This PHP script is working pretty good, but in some cases it generates invalid javascript code, so the Tigra menu won't populate the menu.

Mostly because I need the hide_in_menus -feature (it's an integer field in the database) so I can hide the menu items I want from the structure.Everywhere in the tree: from the begin, from middle, and from the end.

In some cases, if I left the menu item out of printing (hide in menus == 1), the generated javascript code looks like:

['item under this is hidden (let out of printing)', null, null,

['Another', null, null,
['Level 1 Item 0', 'another.html'],
['Level 1 Item 1'],
['Level 1 Item 2'],
['Level 1 Item 3'],

...when this SHOULD BE:

['item under this is hidden (let out of printing)', null, null],
['Another', null, null,
['Level 1 Item 0', 'another.html'],
['Level 1 Item 1'],
['Level 1 Item 2'],
['Level 1 Item 3'],

So as you can see, there's a tag that stays open, even it contains no visible items. It needs line

['item under this is hidden (let out of printing)', null, null],

['item under this is hidden (let out of printing)', null, null,


if there are NO visible ($hide_in_menus == 0) items inside it.

I know this sounds like very complex and hard to understand what I mean, but I have no ideas anymore how to solve this problem. Without this "hiding" feature everything works fine (becouse the count_childs() -function will give a correct result everytime), but I just need that feature.

If anyone can help me to modify the code, or give me ideas (or complete script) to create smarter way to generate Tigra menu items from the "Modified Preorder Tree Traversal" -page structure, I'll be very grateful!

02-12-2006, 01:33 AM
Welcome to the Coding Forums, qin3! :D

I don't want to sound like I'm trying to get rid of you, especially since this is your first post here, but have you tried the Tigra forums (http://www.softcomplex.com/forum/forumdisplay_29/)? The reason I suggest going there is that you'd probably have a better chance of getting your questions answered, since people on that forum are familiar with your software. If you haven't already done so, you might want to use their forum search to see if anyone has run into a similar issue to yours.

Hope this helps. :)