Page 1 of 1

Dynamic menu item creation for "Tigra" (DHTML) men

Posted: Fri Feb 10, 2006 7:49 am
by qin
I have a freeware product called "Tigra menu" in use as javascript/DHTML dropdown menu engine. Tigra Menu homepage

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

And the PHP code I've made for generating javascript menu item file (more info here) is:

Code: Select all

<?php

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)) { 
	$i++;
	if (count($right) > 0) { 
		while ($right[count($right) - 1] < $row[rgt]) { 
			array_pop($right); 
		}
	}

	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.

Why?
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:

Code: Select all

['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:

Code: Select all

['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

Code: Select all

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

Code: Select all

['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!

Posted: Fri Feb 10, 2006 8:51 am
by feyd
Have you looked into JSON maybe?

Posted: Fri Feb 10, 2006 8:56 am
by phpScott
have you tried slowly removing some of the \n you are using to remove any extras, or placing them at the front of the next line that will be added to the script instead of at the end of the previous line?