esta es la BD:
Código SQL:
Ver original
CREATE TABLE IF NOT EXISTS `menu` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `id_parent` INT(11) NOT NULL DEFAULT '0', `link` VARCHAR(255) NOT NULL, `order` INT(11) NOT NULL DEFAULT '0', `title` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL DEFAULT '', `level` tinyint(4) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=24 ; INSERT INTO `menu` (`id`, `id_parent`, `link`, `order`, `title`, `level`) VALUES (12, 0, '', 0, 'Office Furniture', 0), (13, 12, '', 0, 'Chairs', 0), (14, 12, '', 0, 'Work Tables', 0), (15, 12, '', 0, 'Workstations', 0), (16, 12, '', 0, 'Storage', 0), (17, 12, '', 0, 'Conference Tables', 0), (18, 13, '', 0, 'with spagh', 0), (19, 13, '', 0, 'without spagh', 0), (20, 18, '#', 0, 'Triangle', 0), (21, 18, '#', 0, 'Sqare', 0), (22, 19, '#', 0, 'Simple', 0), (23, 19, '#', 0, 'Complex', 0);
Y este el script php:
Código PHP:
$db = new PDO('mysql:dbname=db_menu', 'root', '123456',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
function createsubmenu($item)
{
global $db;
$subsel = $db->prepare('SELECT id, link, title, level FROM menu WHERE id_parent = :parent ORDER BY `order`');
$subsel->execute(array('parent' => $item));
$text = '';
foreach ($subsel as $i) {
$text .= '<li class="menucolor' . ($i['level'] - 1) . '">'
.'<a href="' . htmlspecialchars($i['link']) . '">' . htmlspecialchars($i['title']) . '</a>'
. createsubmenu($i['id']) . '</li>';
}
if ($text == '') {
return '';
}
return '<ul id="red" class="treeview-red">' . $text . '</ul>';
}
echo createsubmenu(0);