SitePoint Sponsor |
|
User Tag List
Results 1 to 11 of 11
-
Oct 7, 2007, 10:52 #1
My this recursive code not working?
Hello,
I have the following table structure and data:
Code:-- -- Table structure for table `category` -- CREATE TABLE `category` ( `categoryid` int(11) NOT NULL auto_increment, `catname` varchar(100) NOT NULL default '', `parentcatid` int(11) NOT NULL default '0', PRIMARY KEY (`categoryid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; -- -- Dumping data for table `category` -- INSERT INTO `category` (`categoryid`, `catname`, `parentcatid`) VALUES (1, 'test', 0), (2, 'son of test', 1), (3, 'test2', 0), (4, 'test3', 0);
PHP Code:<?php
// $parent is the parent of the children we want to see
// $level is increased when we go deeper into the tree,
// used to display a nice indented tree
function display_children($parent, $level) {
// retrieve all children of $parent
$result = mysql_query('SELECT catname as title FROM category '.
'WHERE parentcatid="'.$parent.'";');
// display each child
while ($row = mysql_fetch_array($result)) {
// indent and display the title of this child
echo str_repeat(' ',$level).$row['title']."\n";
// call this function again to display this
// child's children
display_children($row['title'], $level+1);
}
}
display_children(1,0);
?>
Please help.Why It Doesn't Work?!
-
Oct 7, 2007, 10:58 #2
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
I think it may be that it's running indefinately - you haven't told it to ever stop running display_children.
Try this. It only recurses if the child has children of it's own.
PHP Code:<?php
function display_children($parent, $level){
$result = mysql_query('SELECT catname as title FROM category'.
'WHERE parentcatid="'.$parent.'";');
while ($row = mysql_fetch_array($result)){
echo str_repeat(' ',$level).$row['title']."\n";
$result2 = mysql_query('SELECT catname as title FROM category'.
'WHERE parentcatid="'.$parent.'";');
if(mysql_num_rows($result2)){
display_children($row['title'], $level+1);
}
}
}
display_children(1,0);
?>Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Oct 7, 2007, 11:55 #3
Thanks but it doesn't work.
Why It Doesn't Work?!
-
Oct 7, 2007, 12:01 #4
- Join Date
- Aug 2007
- Posts
- 365
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Oct 7, 2007, 12:07 #5
- Join Date
- Aug 2007
- Posts
- 365
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I believe you code should be
PHP Code:function display_children($parent=0, $level=0) {
// retrieve all children of $parent
$result = mysql_query('SELECT categoryid,catname as title FROM category '.
'WHERE parentcatid="'.$parent.'";');
// display each child
while ($row = mysql_fetch_array($result)) {
// indent and display the title of this child
echo str_repeat(' ',$level).$row['title']."\n";
// call this function again to display this
// child's children
display_children($row['categoryid'], $level+1);
}
}
display_children();
I dont know if you realize this but on a HTML page your continous spaces will only show as one space. Try using or put the items in a list <ul>
-
Oct 7, 2007, 12:29 #6
Thank you so much, taliesinnz. It worked like a charm.
Why It Doesn't Work?!
-
Oct 7, 2007, 13:16 #7
- Join Date
- Aug 2007
- Posts
- 365
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No Problem
-
Oct 7, 2007, 16:45 #8
- Join Date
- Aug 2007
- Posts
- 365
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
May I suggest that the way you are doing is not really efficent at all.
Using the data you have you are quering the Database 5 times for 4 records.
It is usually more efficent to do just one query and sort the data using php, then multi-queries. Or use a different format for storing the data.
It is just something to keep in mind when dealing with datasets
-
Oct 7, 2007, 19:45 #9
I don't think the difference is noticeable when having 50 rows maximum. What do you think?
Why It Doesn't Work?!
-
Oct 7, 2007, 20:25 #10
- Join Date
- Aug 2007
- Posts
- 365
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It will depend on your web server, and visitor count etc.
But for 50 rows you will get 51 queries per request, and 1 doing it the other way. If you have 100 visitors at a time, thats 5100queries vs 100
just something to think about..
You could do a few benchmarks, take the average time of the queries over a loop of 500 and see the time diference, then write batch script to do multipul request to log, if you are that interested in results
-
Oct 8, 2007, 01:56 #11
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This article should help you further
http://www.sitepoint.com/article/hie...-data-database
Bookmarks