Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP/Mysql   Nested Menu   UL

Posted on 2009-07-05
7
Medium Priority
?
875 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

610 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