qin
Junior Member
Posts: 2
Registered: 2/13/2006
Location: Finland
Member Is Offline
|
| posted on 2/13/2006 at 09:49 AM |
|
|
Dynamic menu item creation for Tigra menu. HELP!
I Tigra menu (freeware version) in use as javascript/DHTML dropdown menu engine. 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 is:
| Code: |
<?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))."],nn", ($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: |
['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: |
['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: | | ['itemunder this is hidden (let out of printing)', null, null], |
INSTEAD OF
| Code: |
['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!
|
|
|
qin
Junior Member
Posts: 2
Registered: 2/13/2006
Location: Finland
Member Is Offline
|
| posted on 2/17/2006 at 05:08 PM |
|
|
up.
|
|
|
|