
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
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.
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
| Code: |
| Code: |
10x alot i will try it
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>
right ?
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.
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 :)
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'.
sorry - i found a typing error in the function array_SearchKey. It should be:
| Code: |
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 :):):):):):):):):)
Could anybody provide me with a dump of the table with some entries ? Thanks in advance
Are there any better instructions than these?
Where do you call insertItems()?
I don't see that on these posts.
after you have done the query, and loaded $menu with the results for the first time:
| Code: |
Alright. I wil try again.
Thanx.
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.
$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: |
Anybody know of an average turn around time when dynamically loading up to 35,000 nodes in this manner?
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.