Why are my submenus appearing above the main menu?

FairyBusiness
FairyBusiness used Ask the Experts™
on
Hi, I am trying to output the submenus underneath each main menu that they belong to.   It outputs them just not in the right place.  The submenus appear above the regular menu:

function menu() {
	$menus = get_menu();
	$output = "";
	while($menu = mysql_fetch_assoc($menus)) {
		$output .= "<li><span></span><a href=\"" . $menu['blurb'] . ".php?menu=" . urlencode($menu['id']) . "\">" . 
					$menu['title'] . "</a>";
		echo submenus($menu['id']);
		$output .= "</li>";
	}	
	return $output;
}
function submenus($menu_id) {
	$submenus = get_submenu($menu_id);
	$output = "<ul class=\"ul_1\">";
	while ($submenu = mysql_fetch_assoc($submenus)) {
		$output .= "<li><a href=\"" . $submenu['blurb'] . ".php?submenu=" . urlencode($submenu['id']) . "\">" . $submenu['title'] . "</a>";
		// next submenus
		$output .= "</li>";
	}
	$output .= "</ul>";
	return $output;
}

Open in new window


Any suggestions?

http://gowiththemaster.com/tleithoff/test.php
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You have
<ul></ul> elements for submenus but you don't have this element in menu() function.

Author

Commented:
Ok i added some but its still not giving me a desired result.  it just grouped all the submenus together now on top
Commented:
Did you update your test.php? Also it will be good if you can post how you are using for us to see. Meanwhile since you are dealing with categories etc, I would like to give you my class method for unlimited category (I use it for select box tough). You can edit to fit your needs or at least it can give you some idea.

# Get Unlimited Categories For Form Select
	public static function getCategories4FormSelect ($class, $selected, $orderArray, $editValue = NULL, $pushData = NULL, $config = NULL, $parent_id = 0) {
		global $flag;
		
		# Creating filter array
		$data = array();
		$data["parent_id"] = $parent_id;
		
		# If we need to push data for filtering results, check if $parent_id is an array
		if (is_array($pushData)) {
			# Loop $pushData and add it to data filter array
			foreach ($pushData as $key => $value) {
				$data[$key] = $value;
			}
		}
		
		(isset($config) && $config === "epin") ?
		$founds = $class::getData("array", "id, {$selected}, category", $data, $orderArray) :
		$founds = $class::getData("array", "id, {$selected}", $data, $orderArray) ;
		
		if ($founds) {
			foreach ($founds as $found) {
				# Returns $flag as global
				self::getFlag4FormSelect($class,$found->id);
				
				if (isset($config) && $config === "epin" && $found->category == 1) {
					$cikti = "<optgroup label=\"{$flag} {$found->$selected}\">";
				}else {				
					$cikti = "<option value=\"{$found->id}\"";
					if ($editValue == $found->id) $cikti .= " selected";
					$cikti .= ">{$flag} {$found->$selected}</option>";
				}
				
				echo $cikti;
				# Count sub-categories
				$totalRecords = $class::countData(array("parent_id"=>$found->id));
				# If there are sub-categories, call the method again
				($totalRecords > 0) ? self::getCategories4FormSelect($class, $selected, $orderArray, $editValue, $pushData, $config, $found->id) : '';
			}
		}
	}
	
	# Get Flag For getCategories4FormSelect()
	private static function getFlag4FormSelect ($class, $id, $count = 0) {
		global $flag;
		$info = $class::getData("id", "parent_id", $id);
		
		$flag = "-";
		
		if ($info->parent_id != 0) {
			$count++;
			self::getFlag4FormSelect($class,$info->parent_id,$count);
		}else {
			for( $i = 0; $i < $count; $i++) {
				$flag .= " --";
			}
		}
	}

Open in new window


I am waiting more details from you (:
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Author

Commented:
Wow, thats look a bit advanced.  I will have to look at that later in more detail.  Thanks though! But for now, what I am trying to do is recreate a navigation that came with a template.  The navigation in the template is static, and I am trying to make it dynamic with a database.  This is an example of one of the menus I am trying to recreate but with my own added link hrefs:

				<li><span></span><a href="#page_1">About Us</a> 
					<ul class="ul_1">
						<li><a href="#page_more">Kitchen Master</a></li>
						<li><a href="#page_more">Our Designers</a>
							<ul class="ul_2">
								<li class="first"><a href="#page_more">Glenn Ray</a></li>
								<li><a href="#page_more">Phillip Weitlauf</a>
								<li class="end"><a href="#page_more">Alison Wallace</a></li>
							</ul>
						</li>
						<li><a href="#page_more">Why The Master?</a></li>
						<li><a href="#page_more">Portfolio</a></li>
						<li class="end"><a href="#page_more">Privacy/Terms</a></li>
					</ul>
				</li>

Open in new window

Author

Commented:
Dont worry about the 3rd level of submenus.  I am just focusing on the top menu link 'about us' and its direct children.

Author

Commented:
I think its because I am echoing the function in the middle of the other function and the menu() function has to  wait to be echoed because its returning something.  I dont think I can accomplish what I am trying to do. . .

Author

Commented:
I just ended up combining the two functions.  I'll look into that code you posted for sure :)

Commented:
Oh yes, I didn't see that.
echo submenus($menu['id']);

Open in new window

Try to add it to $output variable. While you are processing the function some part of it is not returning the value but printing it.

Still if this wouldn't solve the problem, it would be best if we can get more details about your code with the page you are trying to include / call the function so we can try it out locally.

If it will not solve the problem, please let me know I will code a function similar to yours which will do what you want. I don't know why but sometimes I am having hard times to understand without trying it out on my browsers.

Author

Commented:
I just went back and instead of echoing it I added it to $output and it worked!! thanks :))

Commented:
You figured out yourself (:

If you have to list some data from more than 1 table in your database, better to go with classes and such methods. This will allow you to code only once. 1 such method could gather data from any table in your database as unlimited categories / products etc.. and list it anywhere you want (might need modification if you want it other than select box).

I don't know if you can send me a pm or mail over EE but if you get stuck with some modification please let me know I can gladly help you.

Good luck.

Author

Commented:
I have never known how to message anyone on here.  You can add me to your messenger though:

fairyqueenheaven@hotmail.com

or

fairyqueenheaven@yahoo.com

I'm not good with classes in php yet :(  So I stick with the old fashioned way of doing it. . .

Commented:
Added to MSN (:
I strongly suggest you to start to do your work with classes (of course considering it is not extremely simple project). Working with classes will make your life easier.

Author

Commented:
I know. I tried to work with them in the last project of mine but enough people told me to stop trying to do things I didnt know bc I was just making this complicated for myself.  So i'm coding what I know and learning as I go.  I'm in school and I have a goals set for myself on what I need to learn next!

Commented:
I understand you. I can explain how to you classes in 20 min (: When you have time if you will be online on msn (and when I got time) if you want to learn basics of it just let me know. It is not much different than using functions.

Author

Commented:
Ok, great! Talk to you later  ;)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial