goglu
Newbie
Posts: 1
Registered: 11/22/2002
Member Is Offline
|
| posted on 11/22/2002 at 09:52 PM |
|
|
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
|
|
|
horus_kol
Member
Posts: 13
Registered: 9/11/2002
Member Is Offline
|
| posted on 11/29/2002 at 03:52 PM |
|
|
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.
|
|
|
Menta
Junior Member
Posts: 7
Registered: 12/3/2002
Member Is Offline
|
| posted on 12/3/2002 at 10:03 AM |
|
|
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
|
|
|
horus_kol
Member
Posts: 13
Registered: 9/11/2002
Member Is Offline
|
| posted on 12/3/2002 at 10:24 AM |
|
|
| 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);
|
|
|
|
Menta
Junior Member
Posts: 7
Registered: 12/3/2002
Member Is Offline
|
| posted on 12/3/2002 at 10:29 AM |
|
|
:):)
10x alot i will try it
|
|
|
Menta
Junior Member
Posts: 7
Registered: 12/3/2002
Member Is Offline
|
| posted on 12/3/2002 at 11:01 AM |
|
|
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>
|
|
|
Menta
Junior Member
Posts: 7
Registered: 12/3/2002
Member Is Offline
|
| posted on 12/3/2002 at 12:13 PM |
|
|
OK my mistake is that i dont fech result
right ?
|
|
|
horus_kol
Member
Posts: 13
Registered: 9/11/2002
Member Is Offline
|
| posted on 12/3/2002 at 12:24 PM |
|
|
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.
|
|
|
Menta
Junior Member
Posts: 7
Registered: 12/3/2002
Member Is Offline
|
| posted on 12/3/2002 at 01:56 PM |
|
|
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 :)
|
|
|
horus_kol
Member
Posts: 13
Registered: 9/11/2002
Member Is Offline
|
| posted on 12/3/2002 at 02:13 PM |
|
|
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'.
|
|
|
horus_kol
Member
Posts: 13
Registered: 9/11/2002
Member Is Offline
|
| posted on 12/3/2002 at 02:16 PM |
|
|
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];
}
|
|
|
|
Menta
Junior Member
Posts: 7
Registered: 12/3/2002
Member Is Offline
|
| posted on 12/3/2002 at 02:22 PM |
|
|
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 :):):):):):):):):)
|
|
|
Helmut
Newbie
Posts: 1
Registered: 12/16/2002
Member Is Offline
|
| posted on 12/16/2002 at 10:46 AM |
|
|
Could anybody provide me with a dump of the table with some entries ? Thanks in advance
|
|
|
glerma
Junior Member
Posts: 3
Registered: 10/29/2002
Member Is Offline
|
| posted on 6/9/2003 at 05:40 AM |
|
|
Are there any better instructions than these?
Where do you call insertItems()?
I don't see that on these posts.
|
|
|
horus_kol
Member
Posts: 13
Registered: 9/11/2002
Member Is Offline
|
| posted on 6/9/2003 at 07:02 AM |
|
|
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.
|
|
|
glerma
Junior Member
Posts: 3
Registered: 10/29/2002
Member Is Offline
|
| posted on 6/9/2003 at 06:47 PM |
|
|
Alright. I wil try again.
Thanx.
|
|
|
glerma
Junior Member
Posts: 3
Registered: 10/29/2002
Member Is Offline
|
| posted on 6/9/2003 at 08:30 PM |
|
|
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.
|
|
|
horus_kol
Member
Posts: 13
Registered: 9/11/2002
Member Is Offline
|
| posted on 6/10/2003 at 07:28 AM |
|
|
$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...
|
|
|
coombs
Junior Member
Posts: 4
Registered: 8/14/2006
Member Is Offline
|
| posted on 8/14/2006 at 02:01 PM |
|
|
Anybody know of an average turn around time when dynamically loading up to 35,000 nodes in this manner?
|
|
|
tigra
Administrator
Posts: 1990
Registered: 6/17/2002
Location: US, CO
Member Is Offline
|
| posted on 8/14/2006 at 10:43 PM |
|
|
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.
|
|
|