Solved

PHP/Mysql   Nested Menu   UL

Posted on 2009-07-05
7
845 Views
Last Modified: 2013-12-13
I need format the output of this nested loop routine so I can create an ordered list to use in dynamic menus.
See below for the current (incorrect) output
thanks

function createMenu1($parentid) {
 $Result = mysql_query("SELECT * FROM tblcategory WHERE Status=1 and ParentCategoryID = '".$parentid."'");
 while ($Row = mysql_fetch_assoc($Result)) {
  echo ($parentid != $Row['ParentCategoryID']? '<UL>' : '<LI>');
  echo $Row['CategoryName'];
  createMenu1($Row['CategoryID']);
  echo ($parentid != $Row['ParentCategoryID'] ?  '</LI>' : '</UL>');
 }
 mysql_free_result($Result);
}

Current Output
<UL><LI>HOME<LI>aaa</UL></UL></UL><UL><LI>ABOUT US<LI>ab</UL><LI>bbb<LI>asdfasdf<LI>t</UL></UL></UL><LI>d</UL></UL></UL><UL><LI>SERVICES<LI>dddddd</UL></UL></UL><UL><LI>DISPLAY HOMES<LI>sdsd</UL></UL></UL>      
0
Comment
Question by:joomla
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
ID: 24782885
Try this.

function createMenu1($parentid) {

 $Result = mysql_query("SELECT * FROM tblcategory WHERE Status=1 and ParentCategoryID = '".$parentid."'");

 $output = '';

 if(mysql_num_rows($Result) > 0){

	 $output .= '<ul>';

	 	while ($Row = mysql_fetch_assoc($Result)) {

		  $output .= '<li>'.$Row['CategoryName'];

		  $output .= createMenu1($Row['CategoryID']);

		  $output .= '</li>';

		 }

	 $output .= '</ul>';

	 } 

 mysql_free_result($Result);

 return $output;

}

Open in new window

0
 

Author Comment

by:joomla
ID: 24783026
thanks for the feedback
have you tried this yourself?

won't the line $output=' '; empty the string each time it's recursively called?
i tested it and it doesn't seem to output anything

What I want to see is
<ul>
<li>menu item</li>
<li>menu item with submenu</li>
   <ul>
   <li>submenu item1</li>
   </ul>
<li>menu item</li>
</ul>


regards
Michael
0
 
LVL 9

Accepted Solution

by:
Shahzad Fateh Ali earned 250 total points
ID: 24783056
Hi Michael,

$output=' '; will not affect on recursion.
You will need to call it like

$menu = createMenu1($parentid);
echo $menu;

OR

echo createMenu1($parentid);

This is a tested code and i am using it in my project

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Closing Comment

by:joomla
ID: 31600012
thanks so much
sorry for not doing 'echoing'
perfect solution thank you
0
 

Author Comment

by:joomla
ID: 24783254
Hi shahzadfatehali:
Can you provide some additional help
You may have done this on your project.
I'm was trying to format the list so I could create an accordian menu similar to this.
http://www.dynamicdrive.com/dynamicindex17/ddaccordion.htm

regards
Michael
0
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
ID: 24783694
open a new question and paste you existing code there for further review
0
 

Author Comment

by:joomla
ID: 24783834
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now