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);
And the following PHP code:
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);
?>
Can anyone tell me why the code is not working?
Please help.
Bookmarks