简体   繁体   English

生成多级菜单

[英]generation of multi-level menu

I have a problem with the generated multi-level menu. 我对生成的多层菜单有问题。 I would like each menu level select another color (li class=menucolor). 我希望每个菜单级别选择另一种颜色(li class = menucolor)。 However, I managed to only the first and 2nd level do. 但是,我只能做到第一级和第二级。 I can't separate the second level from the third level. 我无法将第二层与第三层分开。 Please help. 请帮忙。

SQL: 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'),

AND CODE: 和代码:

<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>  
if($j=='1') {
echo '<li class="menucolor0">';
}
elseif($j!='1') {
echo '<li class="menucolor1">';
}

You're only allowing for two cases, either it equals 1 or it doesn't. 您只允许两种情况,要么等于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;

} }

Any direct solution to this would be very slow, I suggest you update your table as such: 任何对此的直接解决方案将非常慢,我建议您这样更新表:

<?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));
}

and here is how you display the menu in the desired order: 以下是按所需顺序显示菜单的方式:

<?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);

don't forget to replace databasename, username and password in both scripts 不要忘记在两个脚本中都替换数据库名称,用户名和密码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM