我对生成的多级菜单有一个问题。我想让每个菜单级别选择另一种颜色(li class=menucolor)。然而,我只做到了第一级和第二级。我不能把第二层和第三层分开。请帮帮忙。
SQL:
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) collate utf8_polish_ci NOT NULL default '',
PRIMARY KEY (`id`)
);
INSERT INTO `menu` (`id`, `id_parent`, `link`, `order`, `title`) VALUES
(1, 0, index.php, 3, 'Index'),
(3, 1, link1.php, 1, 'Art1'),
(4, 1, link2.php, 2, 'Art2'),
(5, 4, link2.php, 6, 'Other art'),
(6, 4, link2.php, 1, 'Art AAA'),
(7, 4, link2.php, 1, 'Art BBB'),
(8, 4, link2.php, 1, 'Art CCC'),和代码:
<ul id="menu_left">
<?php
$menuArray = array();
$query = mysql_query(' select id, id_parent, link, order, title
from menu
order by order asc
');
while($row = mysql_fetch_array($query))
{
$menuArray[] = array( 'id' => $row['id'],
'id_parent' => $row['id_parent'],
'link' => $row['link'],
'order' => $row['order'],
'title' => $row['title']
);
}
function menu($id)
{
global $menuArray;
$hasChildren = false;
$resultArray = array();
if (empty($menuArray))
{
return;
}
foreach ($menuArray as $value)
{
if ($value['id_parent'] == $id)
{
$resultArray[] = $value;
}
}
foreach ($resultArray as $value)
{
if ($hasChildren === false)
{
$hasChildren = true;
if ($value['id_parent'] == 0)
{
$j++
?>
<!-- ul first -->
<?php
}
else
{
?>
<!-- <ul> -->
<?php
}
}
if($j=='1') {
echo '<li class="menucolor0">';
}
elseif($j!='1') {
echo '<li class="menucolor1">';
}
?>
<a href=<?php echo $value['link']; ?>"><?php echo $value['title']; ?></a>
<?php
menu($value['id']);
?>
</li>
<?php
}
if ($hasChildren === true)
{
<!-- </ul> -->
}
}
menu(0);
?>
</ul> 发布于 2012-10-07 18:50:25
对此的任何直接解决方案都将非常慢,我建议您这样更新您的表:
<?php
$db = new PDO('mysql:dbname=databasename', 'username', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$db->prepare('ALTER TABLE `menu` ADD COLUMN `level` TINYINT NOT NULL DEFAULT 0;')->execute();
$db->prepare('UPDATE `menu` SET `level` = 1 WHERE `id_parent` = 0;')->execute();
$select = $db->prepare('SELECT `id` FROM `menu` WHERE `level` = :level');
$update = $db->prepare('UPDATE `menu` SET `level` = :level WHERE `id_parent` = :parent');
for( $i = 1; $i < 100; $i++ ) {
$select->execute(array('level' => $i));
while( ($current = $select->fetch(PDO::FETCH_COLUMN)) !== false )
$update->execute(array('level' => $i + 1, 'parent' => $current));
}下面是按所需顺序显示菜单的方式:
<?php
// connect to database using PDO
$db = new PDO('mysql:dbname=databasename', 'username', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
function createsubmenu( $item )
{
global $db;
// prepare a query to get subitems of the current $item (those with parent = $item)
$subsel = $db->prepare('SELECT id, link, title, level FROM menu WHERE id_parent = :parent ORDER BY `order`');
// run the query
$subsel->execute(array('parent' => $item));
$text = '';
// fetch one row at a time, when no more rows are available $i
// will be false and while ends
while( ($i = $subsel->fetch(PDO::FETCH_OBJ)) !== false ) {
// generate code for the current $item
// will recursively call createsubmenu to add possibly existing subitems
$text .= '<li class="menucolor' . ($i->level - 1) . '">'
.'<a href="' . htmlspecialchars($i->link) . '">' . htmlspecialchars($i->title) . '</a>'
. createsubmenu($i->id) . '</li>';
}
// there were no items for the current call
if( $text == '' )
return '';
// items were found, wrap them in an unordered list
return '<ul>' . $text . '</ul>';
}
// call the function for toplevel items (the ones having parent 0)
echo createsubmenu(0);别忘了在两个脚本中替换数据库名、用户名和密码
发布于 2012-10-07 18:21:27
if($j=='1') {
echo '<li class="menucolor0">';
}
elseif($j!='1') {
echo '<li class="menucolor1">';
}您只允许两种情况,要么等于1,要么不等于1。
switch ($j) {
case 1:
echo '<li class="menucolor0">';
break;
case 2:
echo '<li class="menucolor1">';
break;
case 3:
echo '<li class="menucolor2">';
break;
default:
echo '<li class="menucolor0">;
break;}
https://stackoverflow.com/questions/12767863
复制相似问题