<small id='J69u8'></small><noframes id='J69u8'>

<tfoot id='J69u8'></tfoot>

      <bdo id='J69u8'></bdo><ul id='J69u8'></ul>
  • <i id='J69u8'><tr id='J69u8'><dt id='J69u8'><q id='J69u8'><span id='J69u8'><b id='J69u8'><form id='J69u8'><ins id='J69u8'></ins><ul id='J69u8'></ul><sub id='J69u8'></sub></form><legend id='J69u8'></legend><bdo id='J69u8'><pre id='J69u8'><center id='J69u8'></center></pre></bdo></b><th id='J69u8'></th></span></q></dt></tr></i><div id='J69u8'><tfoot id='J69u8'></tfoot><dl id='J69u8'><fieldset id='J69u8'></fieldset></dl></div>

    1. <legend id='J69u8'><style id='J69u8'><dir id='J69u8'><q id='J69u8'></q></dir></style></legend>

        Mysql/php 树形导航菜单

        时间:2023-10-11

          <tbody id='LkSR0'></tbody>
          • <tfoot id='LkSR0'></tfoot>

            <small id='LkSR0'></small><noframes id='LkSR0'>

              <legend id='LkSR0'><style id='LkSR0'><dir id='LkSR0'><q id='LkSR0'></q></dir></style></legend>
              <i id='LkSR0'><tr id='LkSR0'><dt id='LkSR0'><q id='LkSR0'><span id='LkSR0'><b id='LkSR0'><form id='LkSR0'><ins id='LkSR0'></ins><ul id='LkSR0'></ul><sub id='LkSR0'></sub></form><legend id='LkSR0'></legend><bdo id='LkSR0'><pre id='LkSR0'><center id='LkSR0'></center></pre></bdo></b><th id='LkSR0'></th></span></q></dt></tr></i><div id='LkSR0'><tfoot id='LkSR0'></tfoot><dl id='LkSR0'><fieldset id='LkSR0'></fieldset></dl></div>
                <bdo id='LkSR0'></bdo><ul id='LkSR0'></ul>
                1. 本文介绍了Mysql/php 树形导航菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  需要输出代码:

                  视觉输出:

                  我的 sql 数据库'测试'

                  CREATE TABLE IF NOT EXISTS `menu` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(50) 非空,`link` varchar(100) 非空,`parrent_id` int(11) NOT NULL DEFAULT '0',`status` varchar(4) 非空,主键(`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;——-- 转储表`menu`的数据——插入`menu`(`id`、`name`、`link`、`parrent_id`、`status`)值(1, '家', '#home', 0, '显示'),(2, '关于', '#about', 0, '显示'),(3, '我们的服务', '#service', 0, '显示'),(4, '参与', '#getinvolved', 0, '显示'),(5, '捐助者', '#donors', 0, '显示'),(6, '新闻', '#news', 0, '显示'),(7, '联系我们', '#contact', 0, '显示'),(8, '我们自己', '#我们自己', 2, '秀'),(9, '目标', '#objectives', 2, '显示'),(10, '我们的赞助人', '#patrons', 2, '表演');

                  我的 PHP 代码在这里......

                  ";$sql = "SELECT * from menu where parrent_id=".$parrent;while($res = mysql_fetch_array(mysql_query($sql)))echo "<li><a href='".$res['link']."'>".$res['name']."</a></li>";echo "</ul>";}$sql = "SELECT * from menu where parrent_id =0";echo "
                    ";while($resnew = mysql_fetch_array(mysql_query($sql))){$sql_sub = "SELECT * from menu where parrent_id=".$resnew['id'];$count = mysql_num_rows(mysql_query($sql_sub));如果($count>0){echo "<li><a href='".$resnew['link']."'>".$resnew['name']."</a>";菜单($resnew['id']);echo "</li>";}别的{echo "<li><a href='".$resnew['link']."'>".$resnew['name']."</a></li>";}}echo "</ul>";?>

                  解决方案

                  菜单树

                  一个.创建表类别列表".

                  如果不存在则创建表`categorylist`(`id` int(5) NOT NULL auto_increment,`cname` varchar(25) 整理 utf8_unicode_ci 默认为 NULL,`pid` int(5) 非空,`url` 文本整理 utf8_unicode_ci,`状态` int(1),主键(`id`),关键`pid`(`pid`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=9 ;

                  <小时>

                  B.在类别列表"中插入值

                  INSERT INTO `categorylist`(`id`、`cname`、`pid`、`url`、`status`)值(1, '娱乐', 0, '#',1),(2, '电影', 1, 'http://example.com/movie.php',1),(3, '戏剧', 1, 'http://example.com/drama.php',1),(4, '体育', 0, '#',1),(5, '足球', 4, 'http://example.com/football.php',1),(6, '板球', 4, 'http://example.com/cricket.php',1),(7, '政治', 0, '#',1),(8, '政治新闻', 7, 'http://example.com/politics-news.php',1);

                  <小时>

                  c.样式表

                  .nav ul li { 背景:#f1f1f1;列表样式:无;}ul.dropdown { 位置:相对;宽度:自动;字体:12px Arial、Helvetica、sans-serif;}ul.dropdown li { 浮动:左;缩放:1;高度:30px;填充:6px 2px 0 2px;}ul.dropdown li li { border-right:1px solid #ccc;左边框:1px 实心 #ccc;左边距:-30px;}ul.dropdown a:hover { color:#000;} ul.dropdown a:active { color:#ffa500;}ul.dropdown li a { display:block;填充:4px 8px;颜色:#000;文字装饰:无;字体:粗体 12px Arial、Helvetica、sans-serif;}ul.dropdown li:last-child a { border-right:none;}/* 不适用于 IE */ul.dropdown li:hover { 颜色:#000;背景:#e7e7e7;位置:相对;}ul.dropdown li.hover a { color:#000;}ul.dropdown ul { 文本对齐:左;可见性:隐藏;位置:绝对;左:-10px;顶部:36px;}ul.dropdown ul li { 背景:#f1f1f1;边框底部:1px 实心 #ccc;浮动:无;宽度:120px;高度:25px;}ul.dropdown ul li a { border-right:none;宽度:100%;显示:内联块;颜色:#000;}ul.dropdown ul ul { left:100%;顶部:0;}ul.dropdown li:hover >ul { 可见性:可见;}

                  <小时>

                  d.菜单脚本代码

                   $row['pid'], "name" => $row['cname'], "url" => $row['网址']);}//createTree($arrayCategories, 0);函数 createTree($array, $curParent, $currLevel = 0, $prevLevel = -1) {foreach ($array as $categoryId => $category) {if ($curParent == $category['pid']) {if($category['pid']==0) $class="dropdown";否则 $class="sub_menu";if ($currLevel > $prevLevel) echo " <ul class='$class'> ";if ($currLevel == $prevLevel) echo " </li> ";echo '<li id="'.$categoryId.'" ><a href="'.$category['url'].'"&gt;'.$category['name'].'</a>';if ($currLevel > $prevLevel) { $prevLevel = $currLevel;}$currLevel++;createTree ($array, $categoryId, $currLevel, $prevLevel);$currLevel--;}

                  }if ($currLevel == $prevLevel) echo " </li> </ul> ";}?><div class="nav"><?php如果(mysql_num_rows($result)!=0){php createTree($arrayMenu, 0);}?>

                  Code output needed:

                  Visual Output :

                  My sql Database 'test'

                  CREATE TABLE IF NOT EXISTS `menu` (
                    `id` int(11) NOT NULL AUTO_INCREMENT,
                    `name` varchar(50) NOT NULL,
                    `link` varchar(100) NOT NULL,
                    `parrent_id` int(11) NOT NULL DEFAULT '0',
                    `status` varchar(4) NOT NULL,
                    PRIMARY KEY (`id`)
                  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
                  
                  --
                  -- Dumping data for table `menu`
                  --
                  
                  INSERT INTO `menu` (`id`, `name`, `link`, `parrent_id`, `status`) VALUES
                  (1, 'Home', '#home', 0, 'Show'),
                  (2, 'About', '#about', 0, 'Show'),
                  (3, 'Our services', '#service', 0, 'Show'),
                  (4, 'Get involved', '#getinvolved', 0, 'Show'),
                  (5, 'Donors', '#donors', 0, 'Show'),
                  (6, 'News', '#news', 0, 'Show'),
                  (7, 'Contact Us', '#contact', 0, 'Show'),
                  (8, 'Ourselves', '#ourselves', 2, 'Show'),
                  (9, 'Objectives', '#objectives', 2, 'Show'),
                  (10, 'Our Patrons', '#patrons', 2, 'Show');
                  

                  My PHP CODE is Here....

                  <?php
                    $dbuser="root";
                    $dbpass="";
                    $dbname="test";  //the name of the database
                    $chandle = mysql_connect("localhost", $dbuser, $dbpass)
                      or die("Connection Failure to Database");
                    mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found. " . $dbuser);
                  
                    function menu($parrent) {
                      echo "<ul>";
                      $sql = "SELECT * from menu where parrent_id=".$parrent;
                      while($res = mysql_fetch_array(mysql_query($sql)))
                      echo "<li><a href='".$res['link']."'>".$res['name']."</a></li>";
                      echo "</ul>";
                    }
                    $sql = "SELECT * from menu where parrent_id =0";
                    echo "<ul>";
                    while($resnew = mysql_fetch_array(mysql_query($sql)))
                    {
                      $sql_sub = "SELECT * from menu where parrent_id=".$resnew['id']; 
                      $count = mysql_num_rows(mysql_query($sql_sub));
                      if($count>0)
                      {
                        echo "<li><a href='".$resnew['link']."'>".$resnew['name']."</a>";   
                        menu($resnew['id']);
                        echo "</li>";
                      }
                      else
                      {
                        echo "<li><a href='".$resnew['link']."'>".$resnew['name']."</a></li>";
                      }
                    }
                    echo "</ul>";
                  ?>
                  

                  解决方案

                  Menu Tree

                  a. Create the table "categorylist".

                  CREATE TABLE IF NOT EXISTS `categorylist` (
                  `id` int(5) NOT NULL auto_increment,
                  `cname` varchar(25) collate utf8_unicode_ci default NULL,
                  `pid` int(5) NOT NULL,
                  `url` text collate utf8_unicode_ci,
                  `status` int(1),
                  PRIMARY KEY (`id`),
                  KEY `pid` (`pid`)
                  ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=9 ;
                  


                  b. Insert values in the "categorylist"

                  INSERT INTO `categorylist` (`id`, `cname`, `pid`, `url`, `status`) VALUES
                  (1, 'Entertainment', 0, '#',1),
                  (2, 'Movies', 1, 'http://example.com/movie.php',1),
                  (3, 'Drama', 1, 'http://example.com/drama.php',1),
                  (4, 'Sports', 0, '#',1),
                  (5, 'Football', 4, 'http://example.com/football.php',1),
                  (6, 'Cricket', 4, 'http://example.com/cricket.php',1),
                  (7, 'Politics', 0, '#',1),
                  (8, 'Politics news', 7, 'http://example.com/politics-news.php',1);
                  


                  c. Style sheet

                  .nav ul li { background:#f1f1f1; list-style: none;}
                  ul.dropdown { position:relative; width:auto; font:12px Arial, Helvetica, sans-serif; }
                  ul.dropdown li { float:left; zoom:1; height:30px; padding:6px 2px 0 2px; }
                  ul.dropdown li li { border-right:1px solid #ccc; border-left:1px solid #ccc; margin-left:-30px;}
                  ul.dropdown a:hover { color:#000; } ul.dropdown a:active { color:#ffa500; }
                  ul.dropdown li a { display:block; padding:4px 8px; color:#000; text-decoration:none; font:bold 12px Arial, Helvetica, sans-serif; }
                  ul.dropdown li:last-child a { border-right:none;} /* Doesn't work in IE */
                  ul.dropdown li:hover { color:#000; background:#e7e7e7; position:relative; }
                  ul.dropdown li.hover a { color:#000; }
                  ul.dropdown ul { text-align:left; visibility: hidden; position: absolute; left:-10px; top:36px; }
                  ul.dropdown ul li { background:#f1f1f1; border-bottom:1px solid #ccc; float:none; width:120px; height:25px; }
                  ul.dropdown ul li a { border-right:none; width:100%; display:inline-block; color:#000; }
                  ul.dropdown ul ul { left:100%; top:0; }
                  ul.dropdown li:hover > ul { visibility:visible; }
                  


                  d. Code for menu script

                  <?php
                  
                  $con=mysql_connect("localhost", "root", "") or die('Server connexion not possible.');
                  mysql_select_db("newone",$con) or die('Database connexion not possible.');
                  
                  $qry="SELECT * FROM categorylist" where status=1;
                  $result=mysql_query($qry,$con);
                  
                  
                  $arrayMenu = array();
                  
                  while($row = mysql_fetch_assoc($result)){
                  $arrayMenu[$row['id']] = array("pid" => $row['pid'], "name" => $row['cname'], "url" => $row['url']);
                  }
                  
                  
                  //createTree($arrayCategories, 0);
                  
                  function createTree($array, $curParent, $currLevel = 0, $prevLevel = -1) {
                  
                  foreach ($array as $categoryId => $category) {
                  
                  if ($curParent == $category['pid']) {
                  
                  if($category['pid']==0) $class="dropdown"; else $class="sub_menu";
                  if ($currLevel > $prevLevel) echo " <ul class='$class'> ";
                  
                  
                  if ($currLevel == $prevLevel) echo " </li> ";
                  
                  echo '<li id="'.$categoryId.'" >&lt;a href="'.$category['url'].'"&gt;'.$category['name'].'&lt;/a&gt;';
                  
                  if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }
                  
                  $currLevel++;
                  
                  createTree ($array, $categoryId, $currLevel, $prevLevel);
                  
                  $currLevel--;
                  }
                  </pre>
                  }
                  
                  if ($currLevel == $prevLevel) echo " </li> </ul> ";
                  
                  }
                  ?>
                  
                  <div class="nav">
                  
                  <?php
                  if(mysql_num_rows($result)!=0)
                  {
                  php createTree($arrayMenu, 0);
                  }
                  ?>
                  </div>
                  

                  这篇关于Mysql/php 树形导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 wordpress 的子菜单中单击时子菜单消失 下一篇:Youtube API - 提取视频 ID

                  相关文章

                2. <legend id='kSklo'><style id='kSklo'><dir id='kSklo'><q id='kSklo'></q></dir></style></legend>
                  <i id='kSklo'><tr id='kSklo'><dt id='kSklo'><q id='kSklo'><span id='kSklo'><b id='kSklo'><form id='kSklo'><ins id='kSklo'></ins><ul id='kSklo'></ul><sub id='kSklo'></sub></form><legend id='kSklo'></legend><bdo id='kSklo'><pre id='kSklo'><center id='kSklo'></center></pre></bdo></b><th id='kSklo'></th></span></q></dt></tr></i><div id='kSklo'><tfoot id='kSklo'></tfoot><dl id='kSklo'><fieldset id='kSklo'></fieldset></dl></div>

                    <small id='kSklo'></small><noframes id='kSklo'>

                      <bdo id='kSklo'></bdo><ul id='kSklo'></ul>

                    1. <tfoot id='kSklo'></tfoot>