Skip to content

Nested Menu categories with single MySQL query using PHP?

Last updated on January 17, 2018

Today I am gonna write about Nested Menu Categories with Single MySQL Query, Here I will show you how to create a simple nested menu with single Query and Single Table.

I am giving inline comments so that you can follow easily

id];
	
	// add the menu parent
	$thisRef['parent'] = $data->parent;
	$thisRef['label'] = $data->label;
	$thisRef['link'] = $data->link;
	
   // if there is no parent push it into items array()
   if($data->parent == 0) {
	   $items[$data->id] = &$thisRef;
   } else {
	   $ref[$data->parent]['child'][$data->id] = &$thisRef;
   }
}


function get_menu($items,$class = 'menu') {
	$html = "n
    n"; foreach($items as $key=>$value) { $html.= '
  • '.$value['label']; if(array_key_exists('child',$value)) { $html .= get_menu($value['child'],'child'); } $html .= "
  • n"; } $html .= "
n"; return $html; } echo get_menu($items); ?>
0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments