PHP/MySQL List/Tree Menu

Hi,

I’m a php newb and been pulling my hair out on this for several days and would like to ask for a little help.

I have this function that creates a simple (unordered list) menu from a mysql table but only shows the top level categories:

I need it to show all categories, including subs/sub-subs, in a list based (menu) structure with proper hierarchy.
Like this:
http://www.eplanetdesigns.com/sl/ul.html

The function uses this db table:
http://www.eplanetdesigns.com/sl/db.gif

The main{top level} categories use parent_id of 0. For sub categories, the category_id matches the parent_id but I can’t quite get a grasp on how to make this all work.

Any help would be greatly appreciated.

Thank You

Jim

<?php
function Category_Navigation()
{
	$catid = $GLOBALS['catid'];
	if($GLOBALS['categories_display_order'] == "Priority")
	{  $Order_By_String = "category_priority, category_name"; }else{ $Order_By_String = "category_name"; }
	$fetch_query   = "SELECT category_id, category_name FROM phpkb_categories WHERE parent_id=0 AND category_status='public' 
					 AND category_show='yes' ORDER BY $Order_By_String";
	$query_results = mysql_query($fetch_query, $GLOBALS['connection']) or die("Could not execute the Fetching Browse Categories Query.");
	$number_results= mysql_num_rows($query_results);

	if($number_results > 0)
	{
		while($record = mysql_fetch_array($query_results))
		{
			$category_name = stripslashes($record[category_name]);
			$category_link_string = makelink('category', $record[category_id]);
			
			if($catid == $record[category_id]){
				$li_string = "<li class=\"current\">$category_name</li>";
			}
			else{
				$li_string = "<li><a href=\"$category_link_string\">$category_name</a></li>";
			}
			
			$category_navigation .= $li_string."\n";
		} // End of While-Loop.
		mysql_free_result($query_results);
	}
	$category_navigation = "$category_navigation";
	return $category_navigation;
}
?>
<ul class="menu">
<?php print Category_Navigation(); 
</ul>

There is also this function that is used for a dropdown select box. It has the right heirarchy but no links or list structure as it’s building the 's.

<?php
function List_Fetch_Categories($status,$cat_id="")
{ 
	// Adjust the query below with the proper field names and table name. Do not change the 'id', 'name' and 'parent' aliases in the query
	if($GLOBALS['categories_display_order'] == "Priority")
	{  $Order_By_String = "category_priority, category_name"; }else{ $Order_By_String = "category_name"; }
	$sql = "SELECT category_id AS id, category_name AS name, parent_id AS parent FROM phpkb_categories
			WHERE category_status='$status' AND category_show ='yes' ORDER BY $Order_By_String";
	$result = mysql_query($sql, $GLOBALS['connection']);
	$result or die('<option>' . mysql_error() .'</option>'); // For Debug Purpose
	if(mysql_num_rows($result) > 0)
	{
		while ($row = mysql_fetch_assoc($result))
		{
			$names[$row['id']] = stripslashes($row['name']);
			$cats[$row['id']] = $row['parent'];
		}
		mysql_free_result($result);
		display_options($cat_id, hierarchize($cats, 0), $names);
	}
}

function hierarchize(&$cats, $parent)
{
	$subs = array_keys($cats, $parent);
	$tree = array();
	foreach ($subs as $sub)
	{
		$tree[$sub] = hierarchize($cats, $sub);
	}
	return count($tree) ? $tree : $parent;
}

function display_options($cat_id, &$tree, &$names, $nest = 0)
{
	foreach ($tree as $key => $branch)
	{
		$indent = $nest ? str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $nest) . '- ' : '';
		if(is_array($cat_id)) // Embedded code for Multi Select Options
		{
			$select_category_item = (in_array($key, $cat_id)) ? "selected=\"selected\"":"";
		}
		else // Single select Option
		{ 	
			$select_category_item = ($cat_id == $key) ? "selected=\"selected\"":"";
		}
		echo "<option value=\"$key\" $select_category_item>$indent{$names[$key]}</option>\n";
		if(is_array($branch))
		{
			display_options($cat_id, $branch, $names, $nest + 1);
		}
	}
}
?>

Thank You

J

With your data structure you can not build hierarchy tree in one loop. There need to be some recursion, similar to what you have in the second piece of code.

I realize that, just not experienced enough to put it all together.

Thanks anyways.

J

Sponsor our Newsletter | Privacy Policy | Terms of Service