?
Solved

PHP/Mysql   Nested Menu   UL

Posted on 2009-07-05
7
Medium Priority
?
869 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

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

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

765 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