Support Forum Softcomplex logo
About Us  | Services  | Download  | Order  | Support  | |
| Search | Today's Posts | Stats | Board Rules | Not logged in [ Login | Register ]
go to bottom
Last active: Never

Printable Version | Subscribe | Add to Favourites   Post new thread
Author: Subject: Loading Tree menu dynamically!
goglu
Newbie

Posts: 1
Registered: 11/22/2002
Member Is Offline
posted on 11/22/2002 at 09:52 PM Reply With Quote
Loading Tree menu dynamically!


Hi!

I've seen others having the same problem to load the menu from a database. The solution was to create a text file (and it works). What I need, is to load the menu from SQL querries cause the menu has to be done dynamically (not known in advance). What's the function that I have to modified to do this in hte tree.js file? Is there anyone that done this who could show me the code? Would really appreciate it.

Thanks!

JF
View User's Profile View All Posts By User U2U Member
horus_kol
Member

Posts: 13
Registered: 9/11/2002
Member Is Offline
posted on 11/29/2002 at 03:52 PM Reply With Quote

i've just done this myself.

instead of using the tree.js file, i have included a .php which reads in the information and writes out the menu items:


function insertItem ($menu, $ID, $stop)
{
$item = array_SearchKey($menu, 'ID', $ID);
echo "['". $item['text'] ."', ";
if ($item['URL'] == 'null')
{
echo "null, ";
}
else
{
echo "'". $item['URL'] ."', ";
}

if ($item['child_ID'])
{
$child_ID = explode(",", $item['child_ID']);

if (sizeof($child_ID) > 1)
{
for ($i = 0; $i < sizeof($child_ID)-1; $i++)
{
insertItem($menu, $child_ID[$i], 1);
}

insertItem($menu, $child_ID[sizeof($child_ID)-1], 0);
}
else
insertItem($menu, $child_ID[0], 0);

echo "]";
}
else
{
echo "null]";
}

if ($stop == 0)
{
echo " ";
}
else
{
echo ",";
}
}



just use this code to create the menu:

<?php
$menu = mysql_query("SELECT * FROM menu");

echo "<script language='javascript'>";

insertItem($menu, 1, 0);

echo "</script>";

?>

<script language="javascript" src="javascript/003/treemenu/tree_tpl.js"></script>
<script language="javascript" src="javascript/003/treemenu/tree.js"></script>

<script language="javascript">
new tree(TREE_ITEMS, tree_tpl);
</script>

notes:

array_SearchKey is a function I made which finds a key from an array like this one - $array[$i]['Key'] - where $i is a number.

in the mysql database, I have the following fields:

ID : integer primary key autoincrement;
text : varchar(50);
child_ID : varchar(255);
URL : varchar(255);

child_ID contains ALL of the ID's of the items on the next level as "2,3,10,17" etc.

This requires that your root folder is the first entry in the database.

Any question, feel free to ask.
View User's Profile View All Posts By User U2U Member
Menta
Junior Member

Posts: 7
Registered: 12/3/2002
Member Is Offline
posted on 12/3/2002 at 10:03 AM Reply With Quote
Hmm maybe i made somthing wrong :(


ok i try somthing like that
function array_SearchKey($menu, 'ID', $ID)
{
while ($row = mysql_fetch_array($menu, MYSQL_ASSOC)) {
if ($ID==$row["ID"])
{
$item["text"]=$row["text";]
$item["child_ID"]=$row["child_ID"];
$item["URL"]=$row["URL"];
}
}

}
and use your code but somthing is wrong
can u help me ?
10x
View User's Profile View All Posts By User U2U Member
horus_kol
Member

Posts: 13
Registered: 9/11/2002
Member Is Offline
posted on 12/3/2002 at 10:24 AM Reply With Quote

Code:

function array_SearchKey($arr, $key, $val)
{
// $arr = array to be searched
// $key = key in the array which is to be searched with
// $val = value which is to be searched for


$f = 0; // indicates if value is found

for ($i = 0; $i < sizeof($arr); $i++)
{
$j = $i;

for ($arr[$i][$key] == $val)
{
$f = 1;
break;
}

if ($f == 0)
return NULL;
else
return $arr[$j];
}
}



call this with:

Code:

$item = array_SearchKey($menu, 'ID', $ID);
View User's Profile View All Posts By User U2U Member
Menta
Junior Member

Posts: 7
Registered: 12/3/2002
Member Is Offline
posted on 12/3/2002 at 10:29 AM Reply With Quote
:):)


10x alot i will try it
View User's Profile View All Posts By User U2U Member
Menta
Junior Member

Posts: 7
Registered: 12/3/2002
Member Is Offline
posted on 12/3/2002 at 11:01 AM Reply With Quote
Sry Again but...


OK i try but somthing is wrong again :(
ok what i have:
DB-mysql:
CREATE TABLE menu (
ID tinyint(4) NOT NULL auto_increment,
text varchar(255) NOT NULL default '',
child_ID varchar(255) NOT NULL default '',
URL varchar(255) NOT NULL default '',
PRIMARY KEY (ID)
) TYPE=MyISAM;

INSERT INTO menu VALUES (1, 'Test me', '', 'http://Test');
(OK have root folder)
and script tree.php
<?php


function array_SearchKey($arr, $key, $val)
{
// $arr = array to be searched
// $key = key in the array which is to be searched with
// $val = value which is to be searched for


$f = 0; // indicates if value is found

for ($i = 0; $i < sizeof($arr); $i++)
{
$j = $i;
while ($arr[$i][$key] == $val) //in your code this was wrote if..
{
$f = 1;
break;
}

if ($f == 0)
return NULL;
else
return $arr[$j];
}
}



function insertItem ($menu, $ID, $stop)
{
$item = array_SearchKey($menu, 'ID', $ID);

echo "['". $item['text'] ."', ";

if ($item['URL'] == 'null')
{
echo "null, ";
}
else
{
echo "'". $item['URL'] ."', ";
}

if ($item['child_ID'])
{
$child_ID = explode(",", $item['child_ID']);

if (sizeof($child_ID) > 1)
{
for ($i = 0; $i < sizeof($child_ID)-1; $i++)
{
insertItem($menu, $child_ID[$i], 1);
}

insertItem($menu, $child_ID[sizeof($child_ID)-1], 0);
}
else
insertItem($menu, $child_ID[0], 0);

echo "]";
}
else
{
echo "null]";
}

if ($stop == 0)
{
echo " ";
}
else
{
echo ",";
}
}




echo "<script language=javascript>";
mysql_connect("localhost", "root", "menta2ka") or
die("could not connect");
mysql_select_db("phptree");
$menu = mysql_query("SELECT * FROM menu");
insertItem($menu, 1, 0);

echo "</script>";

?>

<script language="javascript"
src="tree_tpl.js"></script>
<script language="javascript"
src="tree.js"></script>

<script language="javascript">
new tree(TREE_ITEMS, tree_tpl);
</script>
and i got this result:
<script language='javascript'>['', '', null] </script>
<script language="javascript"
src="tree_tpl.js"></script>
<script language="javascript"
src="tree.js"></script>

<script language="javascript">
new tree(TREE_ITEMS, tree_tpl);
</script>
View User's Profile View All Posts By User U2U Member
Menta
Junior Member

Posts: 7
Registered: 12/3/2002
Member Is Offline
posted on 12/3/2002 at 12:13 PM Reply With Quote
OK my mistake is that i dont fech result


right ?
View User's Profile View All Posts By User U2U Member
horus_kol
Member

Posts: 13
Registered: 9/11/2002
Member Is Offline
posted on 12/3/2002 at 12:24 PM Reply With Quote

could be - the mysql_fetch_array loads one row of the result into a variable.

I have done this because I want to minimise the amount of access to the database I need, because I also have to do a standalone version from flatfiles.

Loading everything at once, and then passing arrays around is easiest way for me to work.
View User's Profile View All Posts By User U2U Member
Menta
Junior Member

Posts: 7
Registered: 12/3/2002
Member Is Offline
posted on 12/3/2002 at 01:56 PM Reply With Quote
yee almost is ready :):)


OK a write somthing like:
$menu1 = mysql_query("SELECT * FROM menu");
$arr1=array();
while ($row = mysql_fetch_array($menu1, MYSQL_ASSOC)) {

array_push($arr1,$row);
}
that must make array exacly of your type
and all looks fine exept that itst make only 'root' folder :) myabe i dont understand very wall idea of 'child_ID' what means id of childs of this-> parent or this 'parent' is child of ID?
:P

PS: Sry about my bad English :)
View User's Profile View All Posts By User U2U Member
horus_kol
Member

Posts: 13
Registered: 9/11/2002
Member Is Offline
posted on 12/3/2002 at 02:13 PM Reply With Quote

the 'ID' of your root must be 1.

say you add sub-folders, and their ID's are 2, 3 and 7 (this could happen as you change the menu in the future).

the 'child_ID' of your root should be '2,3,7'.

after filling the array '$arr1' you must set '$menu = $arr1'.
View User's Profile View All Posts By User U2U Member
horus_kol
Member

Posts: 13
Registered: 9/11/2002
Member Is Offline
posted on 12/3/2002 at 02:16 PM Reply With Quote

sorry - i found a typing error in the function array_SearchKey. It should be:

Code:

function array_SearchKey($arr, $key, $val)
{
// $arr = array to be searched
// $key = key in the array which is to be searched with
// $val = value which is to be searched for


$f = 0; // indicates if value is found

for ($i = 0; $i < sizeof($arr); $i++)
{
$j = $i;

if ($arr[$i][$key] == $val) //this is supposed to be if - will work in this version
{
$f = 1;
break;
}
}

if ($f == 0)
return NULL;
else
return $arr[$j];

}
View User's Profile View All Posts By User U2U Member
Menta
Junior Member

Posts: 7
Registered: 12/3/2002
Member Is Offline
posted on 12/3/2002 at 02:22 PM Reply With Quote
YES!!!! :):):):):)


10X alot man its working :):)
And this is code :
<?php



function array_SearchKey($arr, $key, $val)
{
// $arr = array to be searched
// $key = key in the array which is to be searched with
// $val = value which is to be searched for


$f = 0; // indicates if value is found

for ($i = 0; $i < sizeof($arr); $i++)
{
$j = $i;

if ($arr[$i][$key] == $val) //this is supposed to be if - will workin this version
{
$f = 1;
break;
}
}

if ($f == 0)
return NULL;
else
return $arr[$j];

}



function insertItem ($menu, $ID, $stop)
{
$item = array_SearchKey($menu, 'ID', $ID);

echo "['". $item['text'] ."', ";

if ($item['URL'] == 'null')
{
echo "null, ";
}
else
{
echo "'". $item['URL'] ."', ";
}

if ($item['child_ID'])
{
// print $item['child_ID'];
$child_ID = explode(",", $item['child_ID']);

if (sizeof($child_ID) > 1)
{
for ($i = 0; $i < sizeof($child_ID)-1; $i++)
{
insertItem($menu, $child_ID[$i], 1);
}

insertItem($menu, $child_ID[sizeof($child_ID)-1], 0);
}
else
insertItem($menu, $child_ID[0], 0);

echo "]";
}
else
{
echo "null]";
}

if ($stop == 0)
{
echo " ";
}
else
{
echo ",";
}
}




echo "<script language='javascript'> var TREE_ITEMS=[";
mysql_connect("localhost", "username", "yourpass") or
die("could not connect");
mysql_select_db("phptree");
$menu1 = mysql_query("SELECT * FROM menu");
$arr1=array();
while ($row = mysql_fetch_array($menu1, MYSQL_ASSOC)) {

array_push($arr1,$row);
}
//$row = mysql_fetch_row ($menu);
insertItem($arr1, 1, 0);
//var TREE_ITEMS=['Test me', 'http://Test', null] ;</script>
//echo "var TREE_ITEMS = [[
// 'Home', 'http://www.softcomplex.com/']];</script>";
echo "];</script>";
?>

<script language=javascript
src="tree_tpl.js"></script>
<script language=javascript
src="tree.js"></script>

<script language=javascript>
new tree(TREE_ITEMS, tree_tpl);
</script>

Again 10X alot :):):):):):):):):)
View User's Profile View All Posts By User U2U Member
Helmut
Newbie

Posts: 1
Registered: 12/16/2002
Member Is Offline
posted on 12/16/2002 at 10:46 AM Reply With Quote

Could anybody provide me with a dump of the table with some entries ? Thanks in advance
View User's Profile View All Posts By User U2U Member
glerma
Junior Member

Posts: 3
Registered: 10/29/2002
Member Is Offline
posted on 6/9/2003 at 05:40 AM Reply With Quote

Are there any better instructions than these?

Where do you call insertItems()?

I don't see that on these posts.
View User's Profile View All Posts By User U2U Member
horus_kol
Member

Posts: 13
Registered: 9/11/2002
Member Is Offline
posted on 6/9/2003 at 07:02 AM Reply With Quote

after you have done the query, and loaded $menu with the results for the first time:

Code:

$menu = mysql_query("SELECT * FROM menu");

insertItem($menu, 0, 0);


insertItem will now go recursively through the $menu array to create the items file.

the root of the menu has ID=0 in the database (as stated in my first post).

because there is one root, the stop parameter is given a value of zero.
View User's Profile View All Posts By User U2U Member
glerma
Junior Member

Posts: 3
Registered: 10/29/2002
Member Is Offline
posted on 6/9/2003 at 06:47 PM Reply With Quote

Alright. I wil try again.

Thanx.
View User's Profile View All Posts By User U2U Member
glerma
Junior Member

Posts: 3
Registered: 10/29/2002
Member Is Offline
posted on 6/9/2003 at 08:30 PM Reply With Quote

O.K. I must have the $menu array built incorrectly, because I am still not able to get this to work.

Here is my scenario:

1. Oracle Table:
SQL> desc tree_menu
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(38)
TEXT VARCHAR2(255)
CHILD_ID VARCHAR2(255)
URL VARCHAR2(255)


2. After doing my sql query my $menu looks like this:

Array
(
[ID] => 1
[TEXT] => My Home
[CHILD_ID] =>
[URL] => http://www.example.com
)

3. After calling insertItem($menu, 1, 0)
the page is built like this:


<script language='javascript'> var TREE_ITEMS=[['', '', null] ];</script>
<script language=javascript
src="tree_tpl.js"></script>
<script language=javascript
src="tree.js"></script>

<script language=javascript>
new tree(TREE_ITEMS, tree_tpl);
</script>

Notice how the TREE_ITEMS values are blank?

I guess my question is that since I can't use msql_query to build my statment, how does my menu array need to be structured in order for insertItems() to work? In other words, if you do a print_r($menu), what does a typical output look like?

Any ideas or suggestions will be appreciated.

g.
View User's Profile View All Posts By User U2U Member
horus_kol
Member

Posts: 13
Registered: 9/11/2002
Member Is Offline
posted on 6/10/2003 at 07:28 AM Reply With Quote

$menu is actually an array of arrays, indexed with the row number when you do the mysql query (in my example)

In your case, I think you need to do this...

Code:

$menu[0][ID] = 1;
$menu[0][TEXT] = 'My Home';
$menu[0][CHILD_ID] = '';
$menu[0][URL]= 'http://www.example.com';


and so on...
View User's Profile View All Posts By User U2U Member
coombs
Junior Member

Posts: 4
Registered: 8/14/2006
Member Is Offline
posted on 8/14/2006 at 02:01 PM Reply With Quote

Anybody know of an average turn around time when dynamically loading up to 35,000 nodes in this manner?
View User's Profile View All Posts By User U2U Member
tigra
Administrator

Posts: 2049
Registered: 6/17/2002
Location: US, CO
Member Is Offline
posted on 8/14/2006 at 10:43 PM Reply With Quote

With 35K nodes it will not be pretty. You'll need to use the script in "load on demand" mode. We have ready to use sample for Tigra Tree Menu PRO with the PHP server side.
View User's Profile View All Posts By User U2U Member
andy
Newbie

Posts: 1
Registered: 3/19/2011
Location: Jakarta
Member Is Offline
posted on 3/19/2011 at 02:42 AM Reply With Quote

Dear my friend,

I try your source code, like this

<?php
function array_SearchKey($arr, $key, $val)
{
$f = 0; // indicates if value is found
for ($i = 0; $i < sizeof($arr); $i++)
{
$j = $i;
if ($arr[$i][$key] == $val) //this is supposed to be if - will workin this version
{
$f = 1;
break;
}
}

if ($f == 0)
return NULL;
else
return $arr[$j];
}

function insertItem ($menu, $ID, $stop)
{
$item = array_SearchKey($menu, 'ID', $ID);
echo "['". $item['text'] ."', ";

if ($item['URL'] == 'null')
{
echo "null, ";
}
else
{
echo "'". $item['URL'] ."', ";
}

if ($item['child_ID'])
{
// print $item['child_ID'];
$child_ID = explode(",", $item['child_ID']);
if (sizeof($child_ID) > 1)
{
for ($i = 0; $i < sizeof($child_ID)-1; $i++)
{
insertItem($menu, $child_ID[$i], 1);
}

insertItem($menu, $child_ID[sizeof($child_ID)-1], 0);
}
else
insertItem($menu, $child_ID[0], 0);
echo "]";
}
else
{
echo "null]";
}

if ($stop == 0)
{
echo " ";
}
else
{
echo ",";
}
}

//echo "<script language='javascript'> var TREE_ITEMS=[";
mysql_connect("localhost", "root", "panorama") or die("could not connect");
mysql_select_db("cdcol");
$menu1 = mysql_query("SELECT * FROM menu");
$arr1=array();
while ($row = mysql_fetch_array($menu1, MYSQL_ASSOC)) {

array_push($arr1,$row);
}
//$row = mysql_fetch_row ($menu);
insertItem($arr1, 1, 0);
//var TREE_ITEMS=['Test me', 'http://Test', null] ;</script>
//echo "var TREE_ITEMS = [[
// 'Home', 'http://www.softcomplex.com/']];</script>";
//echo "];</script>";
?>

and data like this


insert into menu (ID, text, child_ID, URL)
values('1', 'Test me', '0', 'http://Test');

insert into menu (ID, text, child_ID, URL)
values('2', 'Test 2', '1', 'http://xxx');

insert into menu (ID, text, child_ID, URL)
values('3', 'Test 3', '1', 'http://yy');

but the result like this

['Test me', 'http://Test', null]

Can you help me please...
View User's Profile View All Posts By User U2U Member
Post new thread

Related Links:
Tigra Tree Menu
Product Demonstrations Live Demos:
Frames targeting
Win help style
Free Download Free Download
Free Download Menu Builders
Online Builder
Product Documentation Documentation:
Documenation Page
Support Forum Technical Support
Support Forum
   
Tigra Tree Menu PRO
Product Demonstrations Live Demos
Multiple Trees
Frames Targeting
Icons and Styles Overloading
Event Handling
Performance Test
Version 1.2 features
Product Documentation Documentation
Documenation Page
Change Log
Support Forum Technical Support
Support Forum
Customer Care System
Order Now ORDER NOW!
Add to Cart
View Cart
 

Go To Top


Powered by XMB 1.9.1 Nexus
Developed By Aventure Media & The XMB Group © 2002-2004