Psycho Posted May 23, 2011 Share Posted May 23, 2011 I asked you to "output" your tables (i.e. to a text file) not attach an image. If you want my help, at least take the time to provide the information to me so I don't have to manually enter records. Anyway, after making the correction I had previously asked you to make, the results I get are correct. However, I found one possible problem. If a parent does not have any children associated with it, the parent was not included in the output. I assume that is contrary to what you want. This is easily fixed by making the join a LEFT JOIN. But, that created another issue where a record may not have child data. So, I added another condition to only display the child records if the data isn't empty. Lastly, I added some "tabs" into the HTML code to provide some structure in the output. The code mysql_connect($__DB_SERVER__, $__DB_USER__, $__DB_PASS__) or die(mysql_error()); mysql_select_db($__DB_NAME__) or die(mysql_error()); $query = "SELECT pp.ID, pp.Name, p.pageName, p.pageID FROM parent_pages AS pp LEFT JOIN pages AS p ON p.parentID = pp.ID ORDER BY pp.Name"; $result = mysql_query($query) or die(mysql_query()); //Open the parent UL $menuHTML = "<ul>\n"; $currentParentID = false; while ($row = mysql_fetch_assoc($result)) { if($currentParentID !== $row['ID']) { //This is a new parent page if($currentParentID!==false) { //If not first parent, close last submenu UL and parent LI $menuHTML .= "\t\t</ul>\n"; $menuHTML .= "\t</li>\n"; } $currentParentID = $row['ID']; //Open new parent LI, create link and open submenu UL $menuHTML .= "\t<li class=\"mainnav\">\n"; $menuHTML .= "\t\t<a rel=\"nofollow\" href=\"view.php?id={$row['ID']}\">{$row['Name']}</a>\n"; $menuHTML .= "\t\t<ul>\n"; } //Create submenu LI (if exists) if(!empty($row['pageID'])) { $menuHTML .= "\t\t\t<li class=\"subnav\">"; $menuHTML .= "<a rel=\"nofollow\" class=\"sublink_nav\" href=\"viewpage.php?pageid={$row['pageID']}\">{$row['pageName']}</a>"; $menuHTML .= "</li>\n"; } } //Close last child UL, parent LI and parent UL $menuHTML .= "\t\t</ul>\n"; $menuHTML .= "\t</li>\n"; $menuHTML .= "</ul>\n"; echo $menuHTML; The output <ul> <li class="mainnav"> <a rel="nofollow" href="view.php?id=2">Group</a> <ul> </ul> </li> <li class="mainnav"> <a rel="nofollow" href="view.php?id=3">Spa</a> <ul> </ul> </li> <li class="mainnav"> <a rel="nofollow" href="view.php?id=1">Stre</a> <ul> <li class="subnav"><a rel="nofollow" class="sublink_nav" href="viewpage.php?pageid=1">Site 1</a></li> </ul> </li> <li class="mainnav"> <a rel="nofollow" href="view.php?id=4">Well</a> <ul> <li class="subnav"><a rel="nofollow" class="sublink_nav" href="viewpage.php?pageid=3">Site 3</a></li> <li class="subnav"><a rel="nofollow" class="sublink_nav" href="viewpage.php?pageid=2">Site 2</a></li> </ul> </li> </ul> Here is an image of the rendered output (note I used some slightly different data since you made me type it in). Link to comment https://forums.phpfreaks.com/topic/236970-need-recursive-function-for-menue/page/2/#findComment-1219080 Share on other sites More sharing options...
Fotographer96 Posted May 23, 2011 Author Share Posted May 23, 2011 @mjdamato: Do you know something?? You are the best!! Well, sorry for ignoring your advices, next time I'll try to do it as you say Link to comment https://forums.phpfreaks.com/topic/236970-need-recursive-function-for-menue/page/2/#findComment-1219169 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.