hamza Posted March 16, 2010 Share Posted March 16, 2010 cat |_subcat |_product1 |_product2 |_subcat |_product3 |_product4 below code is creating menu like this shown about. the only one thing i need to have is that i want to put <anchor tag when it add products during execution or run. like product4 should be in anchor tag etc. thanks mysql_connect('localhost','root','root');mysql_select_db('sre'); global $menu_array; //running query $result = mysql_query("SELECT * FROM `tblcatagory`"); //fetch rows to show like tree while ( $row = mysql_fetch_assoc($result) ) { $menu_array[$row['cat_id']] = array('Categoryid'=> $row['cat_id'],'name' => $row['cat_name'],'parent' => $row['parent']); } //recursive function that prints categories as a nested html unorderd list function generate_menu($parent) { $has_childs = false; //this prevents printing 'ul' if we don't have subcategories for this category //make array global global $menu_array; //use global array variable instead of a local variable to lower stack memory requierment foreach($menu_array as $key => $value) { if ($value['parent'] == $parent) { //if this is the first child print '<ul>' if ($has_childs === false) { //don't print '<ul>' multiple times $has_childs = true; echo '<ul id="red" class="treevi ew-red treeview">'; } echo '<li> <span> ' . $value['name'].'</span>'; generate_menu($key); //call function again to generate nested list for subcategories belonging to this category echo '</li>'; } } if ($has_childs === true) echo '</ul>';} echo generate_menu(0); schema ====== /* MySQL Data Transfer Source Host: localhost Source Database: sre Target Host: localhost Target Database: sre Date: 3/16/2010 2:08:56 PM */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for tblcatagory -- ---------------------------- CREATE TABLE `tblcatagory` ( `cat_id` int(10) NOT NULL AUTO_INCREMENT, `parent` int(10) DEFAULT NULL, `cat_name` varchar(255) DEFAULT NULL, `cat_description` varchar(255) DEFAULT NULL, `image` varchar(255) DEFAULT NULL, `date_updated` datetime DEFAULT NULL, PRIMARY KEY (`cat_id`) ) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records -- ---------------------------- INSERT INTO `tblcatagory` VALUES ('1', '0', 'Sports', '<p>sports description</p>', null, '2010-03-16 00:00:00'); INSERT INTO `tblcatagory` VALUES ('2', '1', 'hockey', 'hockey decription', null, '2004-09-04 00:30:35'); INSERT INTO `tblcatagory` VALUES ('3', '1', 'football', 'football description', null, null); INSERT INTO `tblcatagory` VALUES ('4', '0', 'Books', 'sdfasdf', null, '2004-09-17 00:30:38'); INSERT INTO `tblcatagory` VALUES ('5', '4', 'Classics', 'sdfasdf', null, '2004-09-24 00:30:40'); INSERT INTO `tblcatagory` VALUES ('6', '4', 'Historical', 'sadfsadfwe', null, '2004-09-30 00:30:32'); INSERT INTO `tblcatagory` VALUES ('7', '4', 'Horror', 'fefwefwefwef', null, '2004-09-17 00:35:35'); INSERT INTO `tblcatagory` VALUES ('8', '4', 'Mystery, Thriller', '<p>fwefwefv we</p>', null, '2004-09-29 00:00:00'); INSERT INTO `tblcatagory` VALUES ('9', '4', 'Religious, Inspirational', 'fv wefwefwefv we', null, '2004-09-10 00:30:29'); INSERT INTO `tblcatagory` VALUES ('10', '7', 'Detective', 'fwefcv we fwefwef ', null, '2004-09-03 00:30:27'); INSERT INTO `tblcatagory` VALUES ('11', '7', 'Suspense', 'fwecf ew we wefwe e', null, '2004-09-03 00:30:46'); INSERT INTO `tblcatagory` VALUES ('12', '2', 'jjjj', 'jjj', '', '2004-09-29 00:00:00'); INSERT INTO `tblcatagory` VALUES ('13', '2', 'a', 'a', '', '2004-09-29 00:00:00'); INSERT INTO `tblcatagory` VALUES ('14', '2', 'a', 'a', '', '2004-09-29 00:00:00'); INSERT INTO `tblcatagory` VALUES ('15', '2', 'ssssssss', '<p> ssssss</p>', '1096398814.jpg', '2004-09-29 00:00:00'); INSERT INTO `tblcatagory` VALUES ('16', '6', 'ssssssssss', '<p> sssssssssssssssssssssssssssss1313131313</p>', '1096398862.jpg', '2004-09-29 00:00:00'); INSERT INTO `tblcatagory` VALUES ('17', '6', 'ssssssssss', '<p> sssssssssssssssssssssssssssss1313131313</p>', '', '2004-09-29 00:00:00'); INSERT INTO `tblcatagory` VALUES ('18', '3', 'sdfsfsdffsdfs', '<h1> sdfsafsf</h1>', '', '2004-09-29 00:00:00'); INSERT INTO `tblcatagory` VALUES ('19', '8', 'aaaa', '<p> aaaaaaaaaaaaaaaaaaaaaaaaa</p>', '', '2004-09-29 00:00:00'); INSERT INTO `tblcatagory` VALUES ('20', '2', 'aaa', '<p> aaaaaaaaaaaaaaaaaaa</p>', '1096400901.jpg', '2004-09-29 00:00:00'); Quote Link to comment Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 I suggest AJAX or JQuery. You will find dozens of awesome tutorials if you look on Google for it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.