Skip to content

Instantly share code, notes, and snippets.

@ihsanberahim
Last active August 29, 2015 13:57
Show Gist options
  • Save ihsanberahim/11f8d1e1b4b5eba54e5e to your computer and use it in GitHub Desktop.
Save ihsanberahim/11f8d1e1b4b5eba54e5e to your computer and use it in GitHub Desktop.
simple code to create php menu tree. Inspired by Drupal.
<?php
require_once('menu.php');
?>
<?php load_menu() ?>
<table cellpadding="0" cellspacing="0" border="0" class="stdtable">
<colgroup>
<col class="con1">
<col class="con1">
</colgroup>
<thead>
<tr>
<th class="head1">Menu Items</th>
<th class="head1">State</th>
</tr>
</thead>
<tbody>
<?php load_menurow() ?>
</tbody>
<tfoot>
<tr>
<th colspan="2"><button class="stdbtn btn_red" style="float: right;">SAVE</button></th>
</tr>
</tfoot>
</table>
<?php
/**
* @author ihsanberahim
*
Credit:
http://ihsanberahim.com
http://pkmedia.com.my
http://fiction-labs.com
*/
global $db;
$db = new PDO('mysql:host=localhost;dbname=test_menu', 'root', 'root');
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
function load_menu()
{
global $db;
$query = $db->prepare("SELECT * FROM menus WHERE type='group' ORDER BY id");
$query->execute();
$menus = $query->fetchAll();
foreach($menus as $menu)
{
echo '<ul>'.render_tree($menu).'</ul>';
}
}
function load_menurow()
{
global $db;
$query = $db->prepare("SELECT * FROM menus WHERE type='group' ORDER BY id");
$query->execute();
$menus = $query->fetchAll();
foreach($menus as $menu)
{
echo render_tablelist($menu,array(
'before' => '',
'after' => ''
));
}
}
function get_children($menu_id)
{
global $db;
$query = $db->prepare("SELECT * FROM menus WHERE parent_id=:menu_id");
$query->execute(array(
':menu_id' => $menu_id
));
return $query->fetchAll();
}
function render_tree($menu_item,$option=array())
{
$menu_item = (array) $menu_item;
$_option = array(
'sub_before'=>'<ul>',
'sub_after'=>'</ul>',
'before'=>'<li>',
'after'=>'</li>'
);
$option = array_merge($_option,$option);
extract($option,EXTR_SKIP);
$menus = get_children($menu_item['id']);
$output = '';
if($menus)
foreach($menus as $menu)
{
$output .= render_tree((array)$menu);
}
$link_attributes = '';
$link_class = implode(' ',array(
'menu-type-'.$menu_item['type'],
'menu-id-'.$menu_item['id']
));
$link_format = '<a href="%s" class="%s" %s title="%s">%s</a>';
$link = sprintf($link_format,
$menu_item['url'],
$link_class,
$link_attributes,
$menu_item['title'],
$menu_item['title']);
return $before.$link.$sub_before.$output.$sub_after.$after;
}
function render_tablelist($menu_item,$option=array())
{
$menu_item = (array) $menu_item;
$_option = array(
'sub_before'=>'',
'sub_after'=>'',
'before'=>'<td>',
'after'=>'</td>'
);
$option = array_merge($_option,$option);
extract($option,EXTR_SKIP);
$menus = get_children($menu_item['id']);
$has_children = !empty($menus);
$output = '';
if($has_children)
foreach($menus as $menu)
{
$output .= render_tablelist((array)$menu);
}
$row_attributes = '';
$row_class = implode(' ',array(
$menu_item['type'],
'menu-type-'.$menu_item['type'],
'menu-id-'.$menu_item['id']
));
$row_format = '<tr><td class="%s">%s</td><td>%s</td></tr>';
$row = sprintf($row_format,
$row_class,
$menu_item['title'],
$menu_item['status']);
return $row.($has_children ? $sub_before.$output.$sub_after : '');
}
?>
CREATE TABLE `menus` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` text,
`url` text,
`status` varchar(255) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
`order` int(11) DEFAULT NULL,
`type` varchar(255) DEFAULT 'item',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment