ricky spires Posted April 23, 2012 Share Posted April 23, 2012 hello. i finally got my nested list working but its only go 2 levels. how do i make it unlimited levels ? this is a nested list <ul> <li><a href="#">list</a></li> <li><a href="#" class="sub">sub title</a> <ul class="subcat" style="margin-left: 15px"> <li><a href="#">sub title</a></li> </ul> </li> </ul> this is not the actual code but this is how it works at the moment <ul> if($Level == "list"){ <li><a href="#">list</a></li> }elseif($Level == "subList"){ <li><a href="#" class="sub">sub title</a> <ul class="subcat" style="margin-left: 15px"> <li><a href="#">sub title</a></li> </ul> </li> } </ul> Quote Link to comment Share on other sites More sharing options...
xyph Posted April 23, 2012 Share Posted April 23, 2012 You'd have to show us how the data you plan to output is formatted. Is it in a multi-dimensional array? Is it from the database? Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 23, 2012 Share Posted April 23, 2012 When you say unlimited levels, that makes me say recursive function. Quote Link to comment Share on other sites More sharing options...
ricky spires Posted April 24, 2012 Author Share Posted April 24, 2012 thanks for your replies. I have found an example online. i going to study that and play around with it to see if i can work it out. this is the example online that i found http://wizardinternetsolutions.com/web-database-design/single-query-dynamic-multi-level-menu/ thanks ricky Quote Link to comment Share on other sites More sharing options...
ricky spires Posted April 24, 2012 Author Share Posted April 24, 2012 ok. i'm stuck at the first hurdle. this is the site: http://wizardinternetsolutions.com/web-database-design/single-query-dynamic-multi-level-menu/ im stuck getting to info from my database. this is they code they give: <?PHP // Select all entries from the menu table $result=mysql_query("SELECT id, label, link, parent FROM menu ORDER BY listparent, listsort, label"); // Create a multidimensional array to conatin a list of items and parents $menu = array( 'items' => array(), 'listparents' => array() ); // Builds the array lists with data from the menu table while ($items = mysql_fetch_assoc($result)) { // Creates entry into items array with current menu item id ie. $menu['items'][1] echo $menu['items'][$items['id']] = $items; // Creates entry into parents array. Parents array contains a list of all items with children echo $menu['listparents'][$items['listparent']][] = $items['id']; } ?> how can i change that code to work with my system. at the top i have <?PHP require_once("includes/initialize.php"); ?> which gets a the menu class which gets all the database stuff <?PHP require_once(LIB_PATH.DS.'database.php'); class Menu extends DatabaseObject { protected static $table_name="menu"; protected static $db_fields = array( 'id', 'label', 'link', 'linkparent', 'linksort'); public $id; public $label; public $link; public $linkparent; public $linksort; // "new" is a reserved word so we use "make"(or "build") public static function make( $id, $label, $link, $linkparent, $linksort) { if(!empty($id)) { $kw = new Menu(); $kw->id = (int)$id; $kw->label = $label; $kw->link = $link; $kw->linkparent = (int)$linkparent; $kw->linksort = (int)$linksort; return $kw; }else{ return false; } } //end function make public static function find_list(){ $sql = "SELECT id, label, link, linkparent FROM ".self::$table_name." ORDER BY linkparent, linksort, label"; $result_array = self::find_by_sql($sql); return !empty($result_array) ? array_shift($result_array) : false; } } note i had to change parent to linkparent and sort to linksort so from their code i guess i remove this line because its in my class $result=mysql_query("SELECT id, label, link, parent FROM menu ORDER BY listparent, listsort, label"); so how do i get the stuff out of the db into the while loop etc... thanks rick Quote Link to comment Share on other sites More sharing options...
ricky spires Posted April 28, 2012 Author Share Posted April 28, 2012 hello. sorry but i can't work it out. i gives me a list of headers but only the top header gives me any sub levels and its not giving me sub sub levels ... i just does not work. this is the db structure `id', `linkTitle', `listLevel', `parent_id', Header (id=1 - pnt=0 - level=header) subTitle1 (id=2 - pnt=1 - level=title) link1 (id=3 - pnt=1 - level=link) link1 (id=4 - pnt=1 - level=link) link1 (id=5 - pnt=1 - level=link) subTitle2 (id=6 - pnt=2 - level=title) link2 (id=7 - pnt=2 - level=link) link2 (id=8 - pnt=2 - level=link) link2 (id=9 - pnt=2 - level=link) subTitle3 (id=10 - pnt=6 - level=title) link3 (id=11 - pnt=6 - level=link) link3 (id=12 - pnt=6 - level=link) link3 (id=13 - pnt=6 - level=link) subTitle4 (id=14 - pnt=10 - level=title) link4 (id=15 - pnt=10 - level=link) link4 (id=16 - pnt=10 - level=link) link4 (id=17 - pnt=10 - level=link) so it should look like this level 1 = Header level 2 = subTitle1 - link1 - link1 - link1 level 3 = subTitle2 - link1 - link1 - link1 level 4 = subTitle3 - link1 - link1 - link1 and so on.... this is my code. <?PHP echo'<div class="arrowlistmenu">'; $navHead = PhLists::find_all(); foreach($navHead as $navHeads){ $headID = $navHeads->id; $headLevel = $navHeads->listLevel; //IF LEVEL IS HEADER if($headLevel == "header"){ echo'<li><a><h3 class="menuheader expandable">'.$navHeads->linkTitle.'</h3></a></li>'; }else{ echo' <ul class="categoryitems">'; $navLink = PhLists::find_by_child($headID); foreach($navLink as $navLinks){ $id = $navLinks->id; $PNT = $navLinks->parent_id; $Level = $navLinks->listLevel; //IF LEVEL IS LIST echo $Level == "list" ? '<li><a>'.$navLinks->linkTitle.'</a></li>' : $subLink = PhLists::find_by_child($id); foreach($subLink as $subLinks){ $subid = $subLinks->id; $subPNT = $subLinks->parent_id; $subLevel = $subLinks->listLevel; // IF LEVEL IS TITLE echo '<li><a href="#" class="subexpandable">'.$subLinks->linkTitle.'</a> <ul class="subcategoryitems" style="margin-left: 15px"> <li><a>'.$subLinks->linkTitle.'</a></li> </ul> </li>'; } } echo'</ul>'; } } echo'</div>'; ?> the function find_by_child($id); looks like this public static function find_by_child($child){ $sql = "SELECT * FROM ".self::$table_name." WHERE parent_id=".$child.""; $result_array = self::find_by_sql($sql); return $result_array; } any help would be great. thanks 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.