3

无法弄清楚如何使用 while 循环生成此菜单。

这是我的代码示例:

<ul id="nav">
<li><a href="#">Hoofdmenu 1</a>
<ul class="sub">
        <li><a href="#">Submenu 1.1</a></li>
        <li><a href="#">Submenu 1.2</a></li>
        <li><a href="#">Submenu 1.3</a></li>
        <li><a href="#">Submenu 1.4</a></li>
    </ul>
</li>

<li><a href="#">Hoofdmenu 2</a>
    <ul class="sub">
        <li><a href="#">Submenu 2.1</a></li>
        <li><a href="#">Submenu 2.2</a></li>
        <li><a href="#">Submenu 2.3</a></li>
        <li><a href="#">Submenu 2.4</a></li>
    </ul>
</li>
</ul>

我的 dbtable 看起来像:

paginas:
    id
    title
    content
    type

当从父级输入 == id 时,它应该是子菜单。在我的示例中,这是可行的,现在我必须使其具有动态性。大脑无法正常工作。

谢谢你的帮助!

使用代码从数据库获取数据:

<ul id="nav">
<?php
include_once("ond/inc/php/connect.php");
$query = "SELECT * FROM paginas WHERE type = '0'";
$result = mysql_query($query);
while($row = mysql_fetch_object($result)){

echo '<li><a href="?ond='.$row->titel.'">'.$row->titel.'</a>';}
echo '<ul class="sub">';

$query2 = "SELECT * FROM paginas WHERE type = '".$row->id."'";
$result2 = mysql_query($query2);    
while($row2 = mysql_fetch_object($result2))
{
    echo '<li><a href="?ond='.$row2->titel.'">'.$row2->titel.'</a></li>';
}
echo '</ul>'; 
echo '</li>';

?>
</ul>
4

4 回答 4

1

我会做这样的事情:

首先,将您的数据作为一个数组取出并为每个条目循环遍历它。然后运行这样的东西:

$menuArray = array();

if (empty($type)) // If the entry has no "type", then it's a parent
{
    $menuArray[$type]['title'] = $title;
}
else // else, it's a child, so append it to the parent
{
    $menuArray[$type]['subitems'][] = $title;
}

然后,$menuArray通过它循环创建菜单:

?><ul id="nav"><?php
foreach ($menuArray as $item)
{
    ?><li><a href="#">$item['title']</a><?php
    ?><ul class="sub"><?php

    foreach ($item['subitems'] as $subItem)
    {
        <li><a href="#">$subItem</a></li>
    }

    ?></ul><?php
    ?></li><?php
}
?></ul><?php
于 2011-04-07T11:37:08.600 回答
1

下一行做了解决方案:

<ul id="nav">
<?php
include_once("ond/inc/php/connect.php");
$query = "SELECT * FROM paginas WHERE type = '0'";
$result = mysql_query($query);
while($row = mysql_fetch_object($result)){

echo '<li><a href="?ond='.$row->titel.'">'.$row->titel.'</a>';

$query2 = "SELECT * FROM paginas WHERE type = '".$row->id."'";
$result2 = mysql_query($query2);    
echo '<ul class="sub">';
while($row2 = mysql_fetch_object($result2))
{   
    echo '<li><a href="?ond='.$row2->titel.'">'.$row2->titel.'</a></li>';


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



?>
</ul>
于 2011-04-07T12:43:13.373 回答
1
function load()
{
    global $conn;


    $query = "SELECT *  FROM sub_category WHERE main_category_id='1'";
    $result = mysqli_query($conn,$query);
    while($row = mysqli_fetch_assoc($result)){
        $cat_id=$row['sub_category_id'];

    echo '<li><a href="?id='.$row['sub_category_id'].'">'.$row['sub_category_name'].'</a>';

    $query2 = "SELECT *  FROM categories WHERE sub_category_id='$cat_id'";
    $result2 = mysqli_query($conn,$query2);    
    echo '<ul class="sub">';
    while($row2 = mysqli_fetch_assoc($result2))
    {   
        echo '<li><a href="?id='.$row2['category_id'].'">'.$row2['category_name'].'</a></li>';


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


}
于 2019-09-07T20:07:06.387 回答
0

在此示例中使用 parent_id 列并使用引用构建 ul-li 列表 - 它仅进行 1 个 sql 查询并使用递归来呈现输出,可以根据您的需要简单地更改此代码

        <?php
 /**
  * Module for displaying data from items table
  */
class App_Modules_Tree  extends   App_AbstractModule {

    /**
        * array for storing data
        *
        * @var array
        */
    private $tree = array();
       /**
        * html - output of current module
        *
        * @var string
        */
        private $output = ''; 

      /**
       * Retreives data from table items.
       *
       * @return void
       */
        private function _getData()
    {
        $pdo = App_Registry::get('pdo');
        $levels = array();
        foreach ($pdo->query('SELECT * FROM items ORDER BY parent_id ASC',PDO::FETCH_OBJ) as $k=>$v){
                   // references
             $current =  &$levels[ $v->id ] ;
                 $current['parent_id'] = $v->parent_id;
                 $current['name'] = $v->name;
                 if (0 == $v->parent_id){
                 $this->tree[ $v->id ] = &$current;
                 } else {
             $levels[$v->parent_id ]['children'][$v->id] = &$current;
                 }
        }
    }   

    /*
    *App_AbstractModule::preRender overriding
        * @return void
    */
    protected  function preRender()
    {
        $this->_getData();
        }
      /**
       * recursively build html output.
       *
       * @return string
       */
    private function _render($arr)
    {
        $this->output.= '<ul>';
        foreach ($arr as $k=>$v)
        {
            $this->output.= '<li>'.$v['name'].'</li>';
            if( !empty($v['children'])){
                $this->_render($v['children']);
            }
        }
        $this->output.= '</ul>';
        return $this->output;
    }
    /*
    *App_AbstractModule::render overriding
        * @return string
    */
    protected  function render()
    {
            return $this->_render($this->tree);
    }

}
于 2011-04-07T11:38:37.863 回答