Solved

PHP/Mysql   Nested Menu   UL

Posted on 2009-07-05
7
856 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
Independent Software Vendors: 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
 

Author Comment

by:joomla
ID: 24783834
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link failure 16 60
Returning errors to display in a dialog box - Ajax, Json, Php 16 49
Converting PHP triple des decrypt to c# 9 62
How do use '  ' within this code? 4 24
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
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.

680 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