Hi can someone please help me I am completly stuck
I have created a tree walk using recursion.
The data is pulled out from a database with this structure:
id = uniqe id
name = some name
_parent = holds the id which the item/name belongs to
The recursive function looks like this
<?
$link = mysql_connect("localhost", "", "");
mysql_select_db("test");
?>
<?
function buildTree($id = 0, $cnt = 1) {
$query = "SELECT * FROM names WHERE _parent = ".$id."";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo str_repeat("»",$cnt);
$foo = $cnt-1;
echo " ".$row['name']."<br>";
buildTree($row['id'], $cnt + 1);
}
}
?>
<?
buildTree();
?>
I have this javascript menu that needs its items structured like this:
Menu1=new Array("Home","http://www.dynamicdrive.com","",0,20,138);
Menu2=new Array("News","blank.htm","",2);
Menu2_1=new Array("General","blank.htm","",5,20,150);
Menu2_1_1=new Array("CNN","http://www.cnn.com","",0,20,150);
Menu2_1_2=new Array("ABCNews","http://www.abcnews.com","",0);
Menu2_1_3=new Array("MSNBC","http://www.msnbc.com","",0);
Menu2_1_4=new Array("CBSNews","http://www.cbsnews.com","",0);
Menu2_1_5=new Array("Canadian News","http://news.bbc.co.uk","",2);
Menu2_1_5_1=new Array("Vancouver Sun","http://www.vancouversun.com","",0,20,150);
Menu2_1_5_2=new Array("CTV News","http://www.ctvnews.com","",0);
Menu2_2=new Array("Technology","blank.htm","",3);
Menu2_2_1=new Array("TechWeb","http://www.techweb.com","",0,20,200);
Menu2_2_2=new Array("News.com","http://www.news.com","",0);
Menu2_2_3=new Array("Wired News","http://www.wired.com","",0);
As you see the menu has an indexing system
Where the top level is
1
2
2_1
2_1_1
2_1_2
2_1_3
3
3_1
3_2
3_3_1
4
5
and so on ...
How can I create these numbers in my PHP recursion, please help coz I wont give it up, and have now battled with this almost in 2 days.
thanks in advance,
Thomas A
The database table structure and data
drop table if exists names;
CREATE TABLE names (
id int(9) unsigned NOT NULL auto_increment,
name char(255) default NULL,
_parent int(9) default NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;
/
Table data for test.names
/
INSERT INTO names VALUES (1,'Hjem',0);
INSERT INTO names VALUES (2,'Produkter',0);
INSERT INTO names VALUES (3,'Om oss',0);
INSERT INTO names VALUES (4,'FAQ',0);
INSERT INTO names VALUES (5,'Kontakt',0);
INSERT INTO names VALUES (6,'Radioer',2);
INSERT INTO names VALUES (7,'Tv-er',2);
INSERT INTO names VALUES (8,'Videoer',2);
INSERT INTO names VALUES (9,'Vår vision',3);
INSERT INTO names VALUES (10,'Historie',3);
INSERT INTO names VALUES (11,'Partnere',3);
INSERT INTO names VALUES (12,'Ledige stillinger',3);
INSERT INTO names VALUES (13,'Salg',5);
INSERT INTO names VALUES (14,'Support',5);
INSERT INTO names VALUES (15,'Webmaster',5);
INSERT INTO names VALUES (16,'Produkter',4);
INSERT INTO names VALUES (17,'bla bla ...',4);
INSERT INTO names VALUES (18,'Priser',4);
INSERT INTO names VALUES (31,'Gammel',10);
INSERT INTO names VALUES (30,'Moderne',10);