Codice PHP:
<?php
class MenuBuilder {
var $conn;
var $db;
var $items = array();
var $html = array();
function MenuBuilder() {
global $db, $core;
// var_dump($db);
$this->conn = mysql_connect($this->db['server'], $this->db['user'], $this->db['pass']);
mysql_select_db($this->db['database'], $this->conn);
}
function get_menu_items() {
global $db, $core;
$sql = "SELECT id, parent_id, page_id, page_slug, mod_id, name$core->dblang, slug, content_type, link, target, icon, position, home_page, active FROM menus WHERE menus.active = 1 ORDER BY parent_id, position";
return $db->fetch_assoc_all($sql);
}
function get_menu_html($root_id = 0) {
global $db, $core, $content;
$this->html = array();
$this->items = $this->get_menu_items();
foreach ($this->items as $item)
$children[$item['parent_id']][] = $item;
$loop = !empty($children[$root_id]);
$parent = $root_id;
$parent_stack = array();
//
$this->html[] = '<ul class="menu">';
while ($loop && ( ( $option = each($children[$parent]) ) || ( $parent > $root_id ) )) {
if ($option === false) {
$parent = array_pop($parent_stack);
$this->html[] = str_repeat("\t", ( count($parent_stack) + 1 ) * 2) . '[/list]';
$this->html[] = str_repeat("\t", ( count($parent_stack) + 1 ) * 2 - 1) . '';
} elseif (!empty($children[$option['value']['id']])) {
$tab = str_repeat("\t", ( count($parent_stack) + 1 ) * 2 - 1);
$this->html[] = sprintf(
'%1$s[*][url="%2$s"]%3$s[/url]', $tab,
$option['value']['slug'], //
$option['value']['name_en']
);
$this->html[] = $tab . "\t" . '<ul class="submenu">';
array_push($parent_stack, $option['value']['parent_id']);
$parent = $option['value']['id'];
}
else
$url = ($core->seo == 1) ? $core->site_url . '/' . sanitize($option['value']['page_slug'], 50) . '.html' : $url = $core->site_url . '/content.php?pagename=' . sanitize($option['value']['page_slug'], 50);
$this->html[] = sprintf(
'%1$s[*][url="%2$s"]%3$s[/url]', str_repeat("\t", ( count($parent_stack) + 1 ) * 2 - 1), // %1$s = tabulation
$url,
$option['value']['name_en']
);
}
$this->html[] = '[/list]';
return implode("\r\n", $this->html);
}
}
$menu = new MenuBuilder();
echo '<pre>' . htmlentities($menu->get_menu_html()) . '</pŕe>';
?>
struttura sql
Codice PHP:
CREATE TABLE IF NOT EXISTS `menus` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) unsigned NOT NULL DEFAULT '0',
`page_id` int(11) NOT NULL DEFAULT '0',
`page_slug` varchar(50) NOT NULL,
`mod_id` int(6) NOT NULL DEFAULT '0',
`name_en` varchar(100) NOT NULL,
`slug` varchar(100) NOT NULL,
`content_type` varchar(20) NOT NULL,
`link` varchar(255) DEFAULT NULL,
`target` enum('_self','_blank') NOT NULL DEFAULT '_blank',
`icon` varchar(50) DEFAULT NULL,
`position` int(11) NOT NULL DEFAULT '0',
`home_page` tinyint(1) NOT NULL DEFAULT '0',
`active` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `content_id` (`active`),
KEY `parent_id` (`parent_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=25 ;
INSERT INTO `menus` (`id`, `parent_id`, `page_id`, `page_slug`, `mod_id`, `name_en`, `slug`, `content_type`, `link`, `target`, `icon`, `position`, `home_page`, `active`) VALUES
(2, 0, 1, 'Home', 0, 'Home', 'Home', 'page', NULL, '', '', 1, 1, 1),
(3, 0, 15, 'Product', 0, 'Product', 'Product', 'page', NULL, '', '', 6, 0, 1),
(7, 0, 8, 'Company', 0, 'Company', 'Company', 'page', NULL, '', '', 2, 0, 1),
(23, 3, 16, 'Team', 0, 'Team', 'Team', 'page', NULL, '', '', 7, 0, 1),
(20, 7, 17, 'Careers', 0, 'Careers', 'Careers', 'page', NULL, '', '', 4, 0, 1),
(21, 7, 15, 'About-us', 0, 'About us', 'About-us', 'page', NULL, '', '', 3, 0, 1),
(24, 20, 18, 'Testimonial', 0, 'Testimonial', 'Testimonial', 'page', NULL, '', '', 5, 0, 1);
risultato della classe