php2MySQL Posted May 30, 2007 Share Posted May 30, 2007 hiiiiiii, i have a big big big problem,i'm trying to solve it since 5 days :-\ , i have a table(cats) ============= id int(3) PK name varchar(100) parent int(3) ============= And i want to display the data from this table like this: please note that the code must be unlimited sub-categories i need to discussion this issue with you . Thanks. Link to comment https://forums.phpfreaks.com/topic/53519-solved-categories-and-sub-cat/ Share on other sites More sharing options...
btherl Posted May 30, 2007 Share Posted May 30, 2007 Your image link doesn't work for me Link to comment https://forums.phpfreaks.com/topic/53519-solved-categories-and-sub-cat/#findComment-264489 Share on other sites More sharing options...
php2MySQL Posted May 30, 2007 Author Share Posted May 30, 2007 Your image link doesn't work for me is image OK now? Link to comment https://forums.phpfreaks.com/topic/53519-solved-categories-and-sub-cat/#findComment-264497 Share on other sites More sharing options...
Barand Posted May 30, 2007 Share Posted May 30, 2007 see http://www.phpfreaks.com/forums/index.php/topic,88111.msg353905.html#msg353905 Link to comment https://forums.phpfreaks.com/topic/53519-solved-categories-and-sub-cat/#findComment-264502 Share on other sites More sharing options...
php2MySQL Posted May 30, 2007 Author Share Posted May 30, 2007 see http://www.phpfreaks.com/forums/index.php/topic,88111.msg353905.html#msg353905 This is my Table: CREATE TABLE `cats` ( `id` int(9) NOT NULL auto_increment, `name` varchar(255) default NULL, `parent` int(9) default NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=cp1256; INSERT INTO `cats` VALUES ('1', 'Cat1', '-1'); INSERT INTO `cats` VALUES ('2', 'Cat2', '-1'); INSERT INTO `cats` VALUES ('3', 'Cat3', '-1'); INSERT INTO `cats` VALUES ('4', 'sub1', '1'); INSERT INTO `cats` VALUES ('5', 'sub2', '2'); INSERT INTO `cats` VALUES ('6', 'sub3', '3'); INSERT INTO `cats` VALUES ('7', 'subsub1', '4'); INSERT INTO `cats` VALUES ('8', 'subsubsub1', '7'); <?php include("inc/config.php"); $DB=new db; function listSubcats ($parent, $level=0) { global $DB; $res = $DB->exe("SELECT id, name FROM cats WHERE parent = '$parent' ORDER BY name"); while (list($id, $name) = $DB->fetch_row($res)) { $indent = str_repeat('>', $level); echo "<OPTION value='$id'>$indent $name</OPTION>\n"; listSubcats($id, $level+1); } } // End Function tst echo "<SELECT name='cat'>"; $res=$DB->exe("select * from cats"); while($bb=$DB->fetch_array($res)) { listSubcats($bb['parent']); } echo '</SELECT>'; ?> When i run the code its showen as image below: Please note that when the parent is -1 its mean that has no parent. Thanks, Link to comment https://forums.phpfreaks.com/topic/53519-solved-categories-and-sub-cat/#findComment-264514 Share on other sites More sharing options...
Barand Posted May 30, 2007 Share Posted May 30, 2007 replace $res=$DB->exe("select * from cats"); while($bb=$DB->fetch_array($res)) { listSubcats($bb['parent']); } with listSubcats(-1); Link to comment https://forums.phpfreaks.com/topic/53519-solved-categories-and-sub-cat/#findComment-264520 Share on other sites More sharing options...
php2MySQL Posted May 30, 2007 Author Share Posted May 30, 2007 greaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaat Thank youuuuu Barand its work now! Link to comment https://forums.phpfreaks.com/topic/53519-solved-categories-and-sub-cat/#findComment-264528 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.