如何在 php 中迭代 ul 和 li 元素?

发布于 2024-08-24 12:54:42 字数 1783 浏览 4 评论 0原文

我正在 php 中创建动态菜单和子菜单...不知道从哪里开始...我已经编写了所有查询,但需要建议使用 Ulli 元素......可能有可能没有子菜单的子菜单,可能有也可能没有菜单的子菜单......

Menu1
  submenu1
     submenu11
     submenu12
  submenu2
     submenu21
Menu2
  submenu1
     submenu11
     submenu12

任何建议......

编辑:

这是我正在做的事情,

<?php

      $con=mysql_connect("localhost","root","");
      $db=mysql_select_db("testt");

      echo '<ul id="nav">';
      $res=mysql_query("SELECT * FROM menu WHERE PARENT_ID=0");
      while($row=mysql_fetch_array($res))
      {
            echo '<li class="top"><a href="#" class="top_link"><span>'.$row['MENU_NAME'].'</span></a></li>';
            $res1=mysql_query("SELECT * FROM menu WHERE PARENT_ID=1 AND CHILD_ID=".$row['ID']."");
            echo '<ul class="sub">';
            while($row1=mysql_fetch_array($res1))
              {
                 echo '<li><a href="#" class="fly">'.$row1['MENU_NAME'].'</li>';
                 $res2=mysql_query("SELECT * FROM menu WHERE PARENT_ID=2 AND CHILD_ID=".$row1['ID']."");
                 echo '<ul>';
                 while($row2=mysql_fetch_array($res2))
                    {
                        echo '<li><a href="#">'.$row2['MENU_NAME'].'</li>';
                    }
                  echo '</ul>';
              }
              echo '</ul>';
      }
      echo '</ul>';





      ?>

这是我的表格,

alt text http:// img229.imageshack.us/img229/1174/84296639.jpg
我的 CSS 类似乎没有得到应用

I am creating dynamic menus and submenus in php.... Can't know where to start.... I have written all my queries but need suggestion to iterate the result set with Ul and li elements with it.... There may are may not be submenus for submenu and there may or may not be submenus for menus.....

Menu1
  submenu1
     submenu11
     submenu12
  submenu2
     submenu21
Menu2
  submenu1
     submenu11
     submenu12

Any suggestion....

EDIT:

Here is what i am doing,

<?php

      $con=mysql_connect("localhost","root","");
      $db=mysql_select_db("testt");

      echo '<ul id="nav">';
      $res=mysql_query("SELECT * FROM menu WHERE PARENT_ID=0");
      while($row=mysql_fetch_array($res))
      {
            echo '<li class="top"><a href="#" class="top_link"><span>'.$row['MENU_NAME'].'</span></a></li>';
            $res1=mysql_query("SELECT * FROM menu WHERE PARENT_ID=1 AND CHILD_ID=".$row['ID']."");
            echo '<ul class="sub">';
            while($row1=mysql_fetch_array($res1))
              {
                 echo '<li><a href="#" class="fly">'.$row1['MENU_NAME'].'</li>';
                 $res2=mysql_query("SELECT * FROM menu WHERE PARENT_ID=2 AND CHILD_ID=".$row1['ID']."");
                 echo '<ul>';
                 while($row2=mysql_fetch_array($res2))
                    {
                        echo '<li><a href="#">'.$row2['MENU_NAME'].'</li>';
                    }
                  echo '</ul>';
              }
              echo '</ul>';
      }
      echo '</ul>';





      ?>

Here is my table,

alt text http://img229.imageshack.us/img229/1174/84296639.jpg

My CSS Class doesn't seem to get applied

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走过海棠暮 2024-08-31 12:54:42

这就是我的做法:

css文件:

ul.makeMenu, ul.makeMenu ul {
    background-color:#000000;
    cursor:default;
    margin:0px 0px 0px 0px;
    padding:0px 0px 0px 0px;
    width: 180px;
}
ul.makeMenu li {
    background-image:url(../images/marker_rosu.gif);
    background-position:5px center;
    background-repeat:no-repeat;
    color:#D4020B;/*#FEBF01;*/
    list-style-type:none;
    position:relative;
    margin:0px 0px 0px 0px;
    padding:0px 5px 0px 15px;
    line-height:24px;
    text-align:left;
}
ul.makeMenu li>ul {
    display:none;
    position:absolute;
    top:0px;
    left:180px;
}
ul.makeMenu li:hover {
    background-image:url(../images/marker_negru.gif);
    background-position:5px center;
    background-repeat:no-repeat;
    background-color:#D4020B;/*#FEBF01;*/
    color:#FFFFFF;
}
ul.makeMenu {
    display:block;
}
ul.makeMenu li:hover>ul {
    display:block;
}
ul.makeMenu li a {
    color:#D4020B;/*#FEBF01;*/
    display:block;
    width:100%;
    text-decoration:none;
}
ul.makeMenu li a:hover {
    color:#000000;
}
ul.makeMenu li:hover>a {
    color:#000000;
}

产品或其他任何类别表的sql文件:

CREATE TABLE `category` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `idParrent` int(10) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

以及创建菜单的功能:

function getAFP ($id)
    {
        global $conx;
        $sql = sprintf("SELECT * FROM category WHERE idParrent = %d;", quote_smart($id));
        $rs = mysql_query($sql, $conx);
        $arrayResult = array();
        if (mysql_num_rows($rs)) {
            while ($row = mysql_fetch_array($rs)) {
                array_push($arrayResult, $row);
            }
            return $arrayResult;
        } else {
            return 0;
        }
    }

function makeListaCategory ($id)
    {
        $listaCategorii = array();
        $categorii = getAFP($id);
        if (! $categorii) {
            return 0;
        } else {
            foreach ($categorii as $categorie) {
                array_push($listaCategorii, $categorie[2] . "|" . $categorie[0]);
                $subcategorii = makeListaCategory($categorie[0]);
                if ($subcategorii) {
                    foreach ($subcategorii as $subcategorie) {
                        $valori = explode("|", $subcategorie);
                        array_push($listaCategorii, $categorie[2] . ">" . $valori[0] . "|" . $valori[1]);
                    }
                }
            }
        }
        return $listaCategorii;
    }

我希望这会对您有所帮助!

This is how i do:

The css file:

ul.makeMenu, ul.makeMenu ul {
    background-color:#000000;
    cursor:default;
    margin:0px 0px 0px 0px;
    padding:0px 0px 0px 0px;
    width: 180px;
}
ul.makeMenu li {
    background-image:url(../images/marker_rosu.gif);
    background-position:5px center;
    background-repeat:no-repeat;
    color:#D4020B;/*#FEBF01;*/
    list-style-type:none;
    position:relative;
    margin:0px 0px 0px 0px;
    padding:0px 5px 0px 15px;
    line-height:24px;
    text-align:left;
}
ul.makeMenu li>ul {
    display:none;
    position:absolute;
    top:0px;
    left:180px;
}
ul.makeMenu li:hover {
    background-image:url(../images/marker_negru.gif);
    background-position:5px center;
    background-repeat:no-repeat;
    background-color:#D4020B;/*#FEBF01;*/
    color:#FFFFFF;
}
ul.makeMenu {
    display:block;
}
ul.makeMenu li:hover>ul {
    display:block;
}
ul.makeMenu li a {
    color:#D4020B;/*#FEBF01;*/
    display:block;
    width:100%;
    text-decoration:none;
}
ul.makeMenu li a:hover {
    color:#000000;
}
ul.makeMenu li:hover>a {
    color:#000000;
}

the sql file for products or anything else category table:

CREATE TABLE `category` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `idParrent` int(10) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

and the function that create menu:

function getAFP ($id)
    {
        global $conx;
        $sql = sprintf("SELECT * FROM category WHERE idParrent = %d;", quote_smart($id));
        $rs = mysql_query($sql, $conx);
        $arrayResult = array();
        if (mysql_num_rows($rs)) {
            while ($row = mysql_fetch_array($rs)) {
                array_push($arrayResult, $row);
            }
            return $arrayResult;
        } else {
            return 0;
        }
    }

function makeListaCategory ($id)
    {
        $listaCategorii = array();
        $categorii = getAFP($id);
        if (! $categorii) {
            return 0;
        } else {
            foreach ($categorii as $categorie) {
                array_push($listaCategorii, $categorie[2] . "|" . $categorie[0]);
                $subcategorii = makeListaCategory($categorie[0]);
                if ($subcategorii) {
                    foreach ($subcategorii as $subcategorie) {
                        $valori = explode("|", $subcategorie);
                        array_push($listaCategorii, $categorie[2] . ">" . $valori[0] . "|" . $valori[1]);
                    }
                }
            }
        }
        return $listaCategorii;
    }

I hope this will help you!

·深蓝 2024-08-31 12:54:42

编辑:在您澄清问题之前发布。这是一般的递归菜单结构。

<?php
function print_menu($menu)
{
  echo "<ul>\n";
  foreach ($menu as $submenu)
  {
    echo "<li>\n";
    echo "<span>{$submenu['name']}</span>\n";
    if (!empty($submenu['children']))
      print_menu($submenu['children']);
    echo "</li>\n";
  }
  echo "</ul>\n";
}

print_menu(
  array(
    array(
      'name' => 'Menu 1',
      'children' => array(
        array(
          'name' => 'Submenu 11',
          'children' => array(
            array('name' => 'Submenu 111'),
            array('name' => 'Submenu 112')
          )
        ),
      )
    ),
    array(
      'name' => 'Menu 2'
    )
  )
);
?>

Edit: Posted before you clarified your question. Here's a general recursive menu structure.

<?php
function print_menu($menu)
{
  echo "<ul>\n";
  foreach ($menu as $submenu)
  {
    echo "<li>\n";
    echo "<span>{$submenu['name']}</span>\n";
    if (!empty($submenu['children']))
      print_menu($submenu['children']);
    echo "</li>\n";
  }
  echo "</ul>\n";
}

print_menu(
  array(
    array(
      'name' => 'Menu 1',
      'children' => array(
        array(
          'name' => 'Submenu 11',
          'children' => array(
            array('name' => 'Submenu 111'),
            array('name' => 'Submenu 112')
          )
        ),
      )
    ),
    array(
      'name' => 'Menu 2'
    )
  )
);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文