【问题标题】:php mysql vertical menuphp mysql 垂直菜单
【发布时间】:2011-02-12 09:47:08
【问题描述】:

我正在尝试设计一个动态垂直菜单: 我的桌子是:

primary // primary key 
pid  //parent id 0 if none
cid //the actual category id
comment //some comment

问题是我想设计一个 php 函数,在从数据库中读取值后,它应该将其输出到 html 有序列表(如多级嵌套无序列表) 我知道使用递归函数很容易实现 问题是我做不到..我尝试了很多次但徒劳无功 主要问题在于嵌套(在哪里给出列表项以及从哪里开始列表)

如果有人能帮助我,我将非常感激......

好吧,我已经设法编写了一个丑陋的代码: {这里我用了两张表,一张给父母,一张给孩子}

$query="SELECT * FROM parentCat";
$result=mysql_query($query);

echo "<ul id=\"suckertree1\">";

while($row=mysql_fetch_array($result))
{
    $name=$row['comment'];
    $pid=$row['catid'];
    echo "<li><a href=\"#\"> $name</a> ";
    $query="select * from childCat WHERE pid=$pid";
    $subresult=mysql_query($query);
    $af=mysql_num_rows($subresult);
    if($af>0)
    {
        echo "<ul>";

        while($subrow=mysql_fetch_array($subresult))
            {
                $name=$subrow['comment'];
                echo "<li><a href=\"#\"> $name</a> </li>";

            }
            echo "</ul>";
    }
    echo "</li>";
}
echo "</ul>";

它只会显示一个子级别... 我应该怎么做才能让它在无限级别上工作

【问题讨论】:

  • 首先使用字典...然后发布您到目前为止尝试过的代码! (即使它看起来丑陋或设计不好);)

标签: php mysql recursion menu tree


【解决方案1】:

我觉得脚本最适合你

$query = mysql_query("SELECT * FROM menu ORDER BY primary ASC");
$parent=0;
$sub=0
echo "<ul>";//start list
while($menu = mysql_fetch_array($query){
   if($parent != $menu['pid']){//if not seen item before
      if($sub != 0){echo "</ul>";}else{$sub++;}//if not first submenu, close submenu before. If first sub sub++.
      echo "<ul>";}//open submenu
   echo "<li>".$menu[cid]."</li>";//echo item
   if($parent != $menu['pid']){//if not seen before
      $parent = $menu['pid']; //set to seen before so next loop will be recognised
   }
}
echo "</ul>"; //end list

我不知道这是否可行,因为我没有对其进行测试,但它应该向您展示如何完成它的选项。列表的想法:

<ul>
  <li>Item1</li>
   <ul>
    <li>Subitem1</li>
    <li>Subitem2</li>
   </ul>
  <li>Item 2</li>
   <ul>
    <li>Subitem1 of Item2</li>
   </ul>
</ul>

给予:

  • 项目1
    • 子项1
    • 子项2
  • 项目2
    • 项目2的子项目1

【讨论】:

  • ul 作为ul 的子级无效。孩子ul 必须在li
【解决方案2】:

试试这个,它应该可以解决问题。

<?php

$query = "SELECT a.comment parent
               , b.comment child
          FROM menu a
          JOIN menu b
          ON a.primary = b.pid
          ORDER BY a.primary";

$result = mysql_query($query);
$parent = ''; 
echo "<ul>";
foreach ($result as $next) {
   if ($next['parent'] != $parent) {
      if (strlen($parent) > 0) {
         echo "    </ul>";
         echo "  </li>";
      }   
      echo "  <li>" . $next['parent'];
      echo "    <ul>";
   }   
   echo "    <li>" . $next['child'] . "</li>";

   $parent = $next['parent'];
}
echo "    </ul>";
echo "  </li>";
echo "</ul>";

?>

【讨论】:

    【解决方案3】:

    要为关联数组呈现嵌套列表,请尝试以下操作:

    <?php
    $list = array(
        'item-1' => 'test-1',
        'item-2' => 'test-2',
        'item-3' => array(
            'item-3-1' => 'test-3',
            'item-3-2' => array(
                'item-3-2-1' => 'test-4',
                'item-3-2-2' => 'test-5',
            ),
        ),
        'item-4' => 'test-6',
    );
    
    function render_list($list) {
        echo '<ul>';
        foreach ($list as $key => $value) {
            echo '<li>';
            echo $key.':';
            if (is_array($value)) render_list($value);
            else echo $value;
            echo '</li>';
        }
        echo '</ul>';
    }
    
    render_list($list);
    

    这将导致:

    <ul>
        <li>item-1:test-1</li>
        <li>item-2:test-2</li>
        <li>
            item-3:
            <ul>
                <li>item-3-1:test-3</li>
                <li>
                    item-3-2:
                    <ul>
                        <li>item-3-2-1:test-4</li>
                        <li>item-3-2-2:test-5</li>
                    </ul>
                </li>
            </ul>
        </li>
        <li>item-4:test-6</li>
    </ul>
    
    • item-1:test-1
    • item-2:test-2
    • 项目 3:
      • item-3-1:test-3
      • 项目 3-2:
        • item-3-2-1:test-4
        • item-3-2-2:test-5
    • item-4:test-6

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 2015-06-28
      相关资源
      最近更新 更多