Solved

PHP/Mysql   Nested Menu   UL

Posted on 2009-07-05
7
864 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 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