googlit Posted September 23, 2012 Share Posted September 23, 2012 Hi all, Im hoping to get a little help or to be pointed in the right direction, what im trying to do is make a DB led category system that will pull data from a table and publish. hopefully looking something like this: -category1 -subcat1 -subcat2 -catrgory2 -subcat1 -subcat2 -category3 -category4 etc etc etc... my table is set up as so: Column Type NULL Default Comments id int(11) No auto Increment - Primary Key catName Varchar(255) No category Title parentCat int(11) No 0 Parent category ID for Sub Categories catDescrip varchar(255) YES NULL Text description of category catOrder varchar(255) YES NULL List Order of Categories isActive int(11) NO 1 Is Active = 1 Innactive = 0 my current page design for the category menu is this: <div style="float: left" id="my_menu" class="sdmenu"> <div> <span>Category 1</span> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> </div> <div> <span>Category 2</span> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> </div> <div class="collapsed"> <span>Category 3</span> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> </div> <div> <span>Category 4</span> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> </div> </div> any help would be appreciated as i am unsure on how to begin with this. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 23, 2012 Share Posted September 23, 2012 If there is a limit to how deep the categories can go, you can simply join the table to itself a few times. Otherwise, write a recursive function to build the list after you select all of them. Quote Link to comment Share on other sites More sharing options...
googlit Posted September 23, 2012 Author Share Posted September 23, 2012 Hmm, thanks for that, im trying a few things but not getting the correct result, however i am not very compliant with recursive functions..... ill keep trying..... Quote Link to comment Share on other sites More sharing options...
googlit Posted September 23, 2012 Author Share Posted September 23, 2012 (edited) ok, so after a lot of reasearch i have found and compiled a script that does that i want, the problem now exists that i have no idea how to impliment it with my current javascript... the two files are below......... The Function: <?php function hasChild($parent_id) { $sql = "SELECT COUNT(*) as count FROM categories WHERE parent_id = '" . $parent_id . "'"; $qry = mysql_query($sql); $rs = mysql_fetch_array($qry); return $rs['count']; } function CategoryTree($list,$parent,$append) { $list = '<li>'.$parent['name'].'</li>'; if (hasChild($parent['id'])) // check if the id has a child { $append++; $list .= "<ul class='child child".$append."'>"; $sql = "SELECT * FROM categories WHERE parent_id = '" . $parent['id'] . "'"; $qry = mysql_query($sql); $child = mysql_fetch_array($qry); do{ $list .= CategoryTree($list,$child,$append); }while($child = mysql_fetch_array($qry)); $list .= "</ul>"; } return $list; } function CategoryList() { $list = ""; $sql = "SELECT * FROM categories WHERE (parent_id = 0 OR parent_id IS NULL)"; $qry = mysql_query($sql); $parent = mysql_fetch_array($qry); $mainlist = "<ul class='parent'>"; do{ $mainlist .= CategoryTree($list,$parent,$append = 0); }while($parent = mysql_fetch_array($qry)); $list .= "</ul>"; return $mainlist; } ?> and the page code (styles etc) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <link rel="stylesheet" type="text/css" href="sdmenu/sdmenu.css" /> <script type="text/javascript" src="sdmenu/sdmenu.js"> </script> <script type="text/javascript"> // <![CDATA[ var myMenu; window.onload = function() { myMenu = new SDMenu("my_menu"); myMenu.init(); }; // ]]> </script> </head> <body> <!--Navbar test---------------------> <div style="float: left" id="my_menu" class="sdmenu"> <div> <span>one </span> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> </div> <div> <span>two</span> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> </div> <div class="collapsed"> <span>three</span> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> </div> <div> <span>four</span> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> <a href="link1.php">link1</a> </div> </div> </body> </html> Can anyone shed some light on this, i have always had issues with merging the two together and escaping etc.... Edited September 23, 2012 by googlit Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 23, 2012 Share Posted September 23, 2012 You posted in PHP, and haven't said anything about any javascript until just now. 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.