2

我想在 MYSQL 中使用 php 和数据创建动态菜单树。

我在mysql中有表field

CREATE TABLE IF NOT EXISTS sys_menu (
  menu_code varchar(16) COLLATE utf8_unicode_ci NOT NULL,
  menu_name varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
  menu_link varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (menu_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


INSERT INTO sys_menu (menu_code, menu_name, menu_link) VALUES
('01', 'setting', NULL),
('01.01', 'admin', NULL),
('01.01.01', 'add', 'add.php'),
('01.01.02', 'edit', 'edit.php');

最终,HTML 应该像下面的代码。

   <ul>
    <li><a href="">Setting</a>
      <ul>
      <li><a href="">Admin</a>
          <ul>
            <li><a href="add.php">Add</a></li>
            <li><a href="edit.php">Edit</a></li>
          </ul>
      </li>
      </ul>
    </li>
    </ul>

谁能帮我解决这个问题。提前致谢。

4

3 回答 3

3

另一种解决方案,应该能够处理各种大小的列表级别。

$listData[]=array('menu_code' => '01','menu_name' => 'setting', 'menu_link' => null);
$listData[]=array('menu_code' => '01.01','menu_name' =>  'admin', 'menu_link' => null);
$listData[]=array('menu_code' => '01.01.01','menu_name' =>  'add', 'menu_link'   =>'add.php');
$listData[]=array('menu_code' => '01.01.02','menu_name' =>  'edit', 'menu_link' => 'edit.php');
$listData[]=array('menu_code' => '02','menu_name' =>  'people', 'menu_link' => null);
$listData[]=array('menu_code' => '02.01','menu_name' =>  'people_edit', 'menu_link' => 'edit.php');
$listData[]=array('menu_code' => '02','menu_name' => 'people', 'menu_link' => null);

现在的功能

function createListMenu($listData){
    $str ='<ul>';
    $lastListLevel=0;
    $firstRow=true;

    foreach($listData as $row){
        $currentListLevel=count(explode('.',$row['menu_code'])) -1 ; // minus one to correct level to Level 0
        $differenceLevel=$currentListLevel-$lastListLevel;

        // deal with the link
        $hrefValue = is_null($row['menu_link']) ? '' : $row['menu_link'];
        $rootLevel=false;

        if($differenceLevel==0){        // stay on the same list level
            if($firstRow){
                $firstRow=false;
            }else{
                $str .='</li>';
            }
            $str .='<li>';
        }elseif($differenceLevel>0){    // go deeper in list level
            for($j=0;$j<$differenceLevel;$j++){
                $str .='<ul><li>';
            }
        }elseif($differenceLevel<0){    // go higher in list level
            for($j=$differenceLevel;$j<=0;$j++){
               if($j==0){  // root level reached
                   $rootLevel=true;
                   $str .='</li>';
               }else{
                   $str .='</li></ul>';
               }
            }
            $str .= ($rootLevel) ? '<li>' : '<ul>';
        }
        $str.='<a href="'.$hrefValue.'">'.$row['menu_name'].'</a>';
        $lastListLevel=$currentListLevel;
    }

    // set end list tags
    for($j=$lastListLevel;$j>=0;$j--){
        $str .='</li></ul>';
    }
    return $str;
}

echo createListMenu($listData);
于 2012-09-20T08:18:21.873 回答
1

数据库设计完全错误,这样做会有很多问题。

相反,您可以更改为parent类似的设计:

CREATE TABLE IF NOT EXISTS `sys_menu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `menu_name` varchar(64) NOT NULL,
  `menu_link` varchar(255) NOT NULL,
  `parent` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


INSERT INTO `sys_menu` (`id`, `menu_name`, `menu_link`, `parent`) VALUES
(1, 'setting', NULL, 0),
(2, 'admin', NULL, 1),
(3, 'add', 'add.php', 2),
(4, 'edit', 'edit.php', 2);

然后你可以制作这样的菜单:

$db = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

function checkChilds($rows, $id) {
    foreach ($rows as $row) {
        if ($row['parent'] == $id) {
            return true;
        }
    }
    return false;
}
function createMenu($rows, $parent=0){  
    $result = "<ul>";
    foreach ($rows as $row){
        if ($row['parent'] == $parent){
            if($row['menu_link']){
                $result .= "<li><a href='".$row['menu_link']."' />".$row['menu_name']."</a>\n";
            } else {
                $result .= "<li>".$row['menu_name']."\n";
            }
            if (checkChilds($rows, $row['id'])){
                $result.= createMenu($rows,$row['id']);
            }
            $result .= "</li>\n";
        }
    }
    $result .= "</ul>\n";

    return $result;
}

$stmt = $db->query("SELECT * FROM `sys_menu`");
$menu = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo createMenu($menu);
于 2012-09-20T07:48:31.693 回答
0
<?php
    $connection=mysql_connect(DB_Server,DB_User,User_Pass);
            $db=mysql_select_db(DB_Name);
    $sql="select * from sys_menu order by menu_code";
    $res=mysql_query($sql) or die(mysql_error());
    echo '<ul>';
    while($row=mysql_fetch_array($res))
        {
            $cur=$row['menu_code'];
            if (strlen($cur)==2)
            {
                echo '<li><a href="">'.$row['menu_name'].'</a><li>';

                $res2=mysql_query($sql) or die(mysql_error());
                echo '<ul>';
                $mainpar=$cur;
                while($row=mysql_fetch_array($res2))
                    {
                        $cur=$row['menu_code'];
                        if ((strlen($cur)==5)&&(substr($cur,0,2)==$mainpar))
                        {
                            echo '<li><a href="">'.$row['menu_name'].'</a><li>';
                            $res3=mysql_query($sql) or die(mysql_error());
                            echo '<ul>';
                            $secpar=$cur;
                            while($row=mysql_fetch_array($res3))
                                {
                                    $cur=$row['menu_code'];
                                    if ((strlen($cur)==8)&&(substr($cur,0,5)==$secpar))
                                    {
                                        echo '<li><a href="'.$row['menu_link'].'">'.$row['menu_name'].'</a><li>';
                                    }   
                                 }
                                 echo '</ul>';
                        }
                    }
                    echo '</ul>';
            }
        }
        echo '</ul>';
?>
于 2012-09-20T07:58:16.303 回答