Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正确排序和设置菜单格式_Php - Fatal编程技术网

Php 正确排序和设置菜单格式

Php 正确排序和设置菜单格式,php,Php,我有个问题。我想从数据库中创建我的导航(主菜单) 它可以很好地工作,并且可以处理无限的父项。唯一的问题是格式 输出应如下所示: <li class="sub"> <a class="widgets" href="#">Einstellungen</a> <ul> <li class="sub"> <a href="#">Allgemeines<span>6

我有个问题。我想从数据库中创建我的导航(主菜单)

它可以很好地工作,并且可以处理无限的父项。唯一的问题是格式

输出应如下所示:

<li class="sub">
    <a class="widgets" href="#">Einstellungen</a>
    <ul>
        <li class="sub"> 
            <a href="#">Allgemeines<span>6</span></a>
            <ul>
                <li><a href="">test1</a></li>
            </ul>
        </li> 
        <li><a href="">test2</a></li>             
    </ul>
</li>   
更新:

Array
(
    [1] => Array
        (
            [parent_id] => 0
            [name] => Hauptseite
            [children] => Array
                (
                )

        )

    [2] => Array
        (
            [parent_id] => 0
            [name] => Einstellungen
            [children] => Array
                (
                    [3] => Array
                        (
                            [parent_id] => 2
                            [name] => Allgemeines
                            [children] => Array
                                (
                                    [4] => Array
                                        (
                                            [parent_id] => 3
                                            [name] => test1
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [5] => Array
                        (
                            [parent_id] => 2
                            [name] => test2
                            [children] => Array
                                (
                                )

                        )

                )

        )

)
function buildList($testArray) {

    echo '<ul>';
    foreach ($testArray as $id => $menuItem):
        if (isset($menuItem['parent_id']) && $menuItem['parent_id'] == 0): // check if it's a parent category
            echo '<li class="home"><a class="widgets" href="#">' . $menuItem['name'] . ' (' . $id . ')' . '</a>';
            if (isset($menuItem['children'])): // if it has children let's spit them out too
                echo '<ul class="child">';
                outputChildren($menuItem['children']);
                echo '</ul>';

            endif;

        endif;


        if (isset($menuItem['parent_id']) && $menuItem['parent_id'] != 0): // if it's a child let's get it's info
            echo '<li class="sub"><a href="#">' . $menuItem['name'] . '</a>';
            if (isset($menuItem['children'])): // if there are grandchildren let's get them too
                echo '<ul class="child">';

                outputChildren($menuItem['children']);
                echo '</ul>';
            endif;
            echo '</li>';
            continue;
        endif;

        if (isset($menuItem['parent_id']) && $menuItem['parent_id'] == 0): // check if it's a parent category
            echo '</li>';
        endif;
    endforeach;
    echo '</ul>';
}

function outputChildren($child) {

    if (isset($child['name'])):
        echo '<li class="sub"><a href="#">' . $child['name'] . '</a>';
        if (!empty($child['children'])):

            echo '<ul>';
            outputChildren($child['children']);
            echo '</ul>';
        endif;
        echo '</li>';
    endif;
}

$sites = array();

$sql = 'SELECT menu_id, menu_name, parent_id FROM `' .$_config['prefix']. '_menu`';

$result = $db->query($sql);
while ($row = $result->fetch_assoc()) {
    $sites[(int) $row['menu_id']] = array(
        'parent_id' => (int) $row['parent_id'],
        'name' => $row['menu_name'],
        'children' => array()
    );
}

foreach ($sites as $site_id => $site) {
    $sites[$site['parent_id']]['children'][$site_id] = & $sites[$site_id];
}

$sites = $sites[0]['children'];
print_r($sites);


echo buildList($sites);
 <ul>
     <li class="home">
         <a class="widgets" href="#">Hauptseite (1)</a>
         <ul class="child"></ul>
     </li>
     <li class="home">
         <a class="widgets" href="#">Einstellungen (2)</a>
         <ul class="child"></ul>
     </li>
 </ul>
函数构建列表($testArray){
回声“
    ”; foreach($id=>$menuItem的testArray): if(isset($menuItem['parent\u id'])&&&$menuItem['parent\u id']==0)://检查它是否是父类别 echo'
  • ; if(isset($menuItem['children'])://如果它有孩子,我们也把他们吐出来 echo'
      ; outputChildren($menuItem['children']); 回声“
    ”; endif; endif; if(isset($menuItem['parent\u id'])&&$menuItem['parent\u id']!=0)://如果它是一个孩子,让我们来获取它的信息 回显“
  • ”; if(isset($menuItem['children'])://如果有孙子,我们也要他们 echo'
      ; outputChildren($menuItem['children']); 回声“
    ”; endif; 回音“
  • ”; 继续; endif; if(isset($menuItem['parent\u id'])&&&$menuItem['parent\u id']==0)://检查它是否是父类别 回音“”; endif; endforeach; 回声“
”; } 函数outputChildren($child){ 如果(isset($child['name']): 回显“
  • ”; 如果(!empty($child['children']): 回声“
      ”; 输出儿童($child['children']); 回声“
    ”; endif; 回音“
  • ”; endif; } $sites=array(); $sql='从`.$\u配置['prefix']中选择菜单id、菜单名称、父项id。'_菜单`; $result=$db->query($sql); 而($row=$result->fetch_assoc()){ $sites[(int)$row['menu_id']]=array( 'parent_id'=>(int)$row['parent_id'], “名称”=>$row[“菜单名称”], 'children'=>array() ); } foreach($site作为$site\u id=>$site的站点){ $sites[$site['parent_id']['children'][$site_id]=&$sites[$site_id]; } $sites=$sites[0]['children']; 打印(站点); echo构建列表($站点);
    将输出:

    Array
    (
        [1] => Array
            (
                [parent_id] => 0
                [name] => Hauptseite
                [children] => Array
                    (
                    )
    
            )
    
        [2] => Array
            (
                [parent_id] => 0
                [name] => Einstellungen
                [children] => Array
                    (
                        [3] => Array
                            (
                                [parent_id] => 2
                                [name] => Allgemeines
                                [children] => Array
                                    (
                                        [4] => Array
                                            (
                                                [parent_id] => 3
                                                [name] => test1
                                                [children] => Array
                                                    (
                                                    )
    
                                            )
    
                                    )
    
                            )
    
                        [5] => Array
                            (
                                [parent_id] => 2
                                [name] => test2
                                [children] => Array
                                    (
                                    )
    
                            )
    
                    )
    
            )
    
    )
    
    function buildList($testArray) {
    
        echo '<ul>';
        foreach ($testArray as $id => $menuItem):
            if (isset($menuItem['parent_id']) && $menuItem['parent_id'] == 0): // check if it's a parent category
                echo '<li class="home"><a class="widgets" href="#">' . $menuItem['name'] . ' (' . $id . ')' . '</a>';
                if (isset($menuItem['children'])): // if it has children let's spit them out too
                    echo '<ul class="child">';
                    outputChildren($menuItem['children']);
                    echo '</ul>';
    
                endif;
    
            endif;
    
    
            if (isset($menuItem['parent_id']) && $menuItem['parent_id'] != 0): // if it's a child let's get it's info
                echo '<li class="sub"><a href="#">' . $menuItem['name'] . '</a>';
                if (isset($menuItem['children'])): // if there are grandchildren let's get them too
                    echo '<ul class="child">';
    
                    outputChildren($menuItem['children']);
                    echo '</ul>';
                endif;
                echo '</li>';
                continue;
            endif;
    
            if (isset($menuItem['parent_id']) && $menuItem['parent_id'] == 0): // check if it's a parent category
                echo '</li>';
            endif;
        endforeach;
        echo '</ul>';
    }
    
    function outputChildren($child) {
    
        if (isset($child['name'])):
            echo '<li class="sub"><a href="#">' . $child['name'] . '</a>';
            if (!empty($child['children'])):
    
                echo '<ul>';
                outputChildren($child['children']);
                echo '</ul>';
            endif;
            echo '</li>';
        endif;
    }
    
    $sites = array();
    
    $sql = 'SELECT menu_id, menu_name, parent_id FROM `' .$_config['prefix']. '_menu`';
    
    $result = $db->query($sql);
    while ($row = $result->fetch_assoc()) {
        $sites[(int) $row['menu_id']] = array(
            'parent_id' => (int) $row['parent_id'],
            'name' => $row['menu_name'],
            'children' => array()
        );
    }
    
    foreach ($sites as $site_id => $site) {
        $sites[$site['parent_id']]['children'][$site_id] = & $sites[$site_id];
    }
    
    $sites = $sites[0]['children'];
    print_r($sites);
    
    
    echo buildList($sites);
    
     <ul>
         <li class="home">
             <a class="widgets" href="#">Hauptseite (1)</a>
             <ul class="child"></ul>
         </li>
         <li class="home">
             <a class="widgets" href="#">Einstellungen (2)</a>
             <ul class="child"></ul>
         </li>
     </ul>
    
        请参见第一个示例


        谢谢更新

        这是我们的测试阵列:

        $test = Array([1] => Array(
                    'parent_id' => 0,
                    'name' => Hauptseite,
                    'children' => Array()),
                    [2] => Array(
                    'parent_id' => 0,
                    'name' => Einstellungen,
                    'children' => Array([3] => Array(
                                            'parent_id' => 2,
                                            'name' => 'Allgemeines',
                                            'children' => Array([4] => Array(
                                                            'parent_id' => 3,
                                                            'name' => 'test1',
                                                            'children' => Array())
                                                            )
                                                    ),
                                        [5] => Array(
                                            'parent_id' => 2,
                                            'name' => 'test2',
                                            'children' => Array()
                                                    )
                                        )
                        )
        
                );
        
        为了获得预期结果,我们将采取以下步骤:

        1) 如果父Id=0,则为父Id(否则为子Id)

        2) 如果
        isset($item['children'])
        那么我们需要吐出孩子(可能还有孙子)

        代码如下:

        function buildList($testArray) {
        
            echo '<ul>';
            foreach ($testArray as $id => $menuItem):
                if (isset($menuItem['parent_id']) && $menuItem['parent_id'] == 0): // check if it's a parent category
                    echo '<li class="home"><a class="widgets" href="#">' . $menuItem['name'] . ' (' . $id . ')' . '</a>';
                    if (isset($menuItem['children'])): // if it has children let's spit them out too
                        echo '<ul class="child">';
                        outputChildren($menuItem['children']);
                        echo '</ul>';
        
                    endif;
        
                endif;
        
        
                if (isset($menuItem['parent_id']) && $menuItem['parent_id'] != 0): // if it's a child let's get it's info
                    echo '<li class="sub"><a href="#">' . $menuItem['name'] . '</a>';
                    if (isset($menuItem['children'])): // if there are grandchildren let's get them too
                        echo '<ul class="child">';
        
                        outputChildren($menuItem['children']);
                        echo '</ul>';
                    endif;
                    echo '</li>';
                    continue;
                endif;
        
                if (isset($menuItem['parent_id']) && $menuItem['parent_id'] == 0): // check if it's a parent category
                    echo '</li>';
                endif;
            endforeach;
            echo '</ul>';
        }
        
        function outputChildren($child) {
        
            if (isset($child['name'])):
                echo '<li class="sub"><a href="#">' . $child['name'] . '</a>';
                if (!empty($child['children'])):
        
                    echo '<ul>';
                    outputChildren($child['children']);
                    echo '</ul>';
                endif;
                echo '</li>';
            endif;
        }
        
        函数构建列表($testArray){
        回声“
          ”; foreach($id=>$menuItem的testArray): if(isset($menuItem['parent\u id'])&&&$menuItem['parent\u id']==0)://检查它是否是父类别 echo'
        • ; if(isset($menuItem['children'])://如果它有孩子,我们也把他们吐出来 echo'
            ; outputChildren($menuItem['children']); 回声“
          ”; endif; endif; if(isset($menuItem['parent\u id'])&&$menuItem['parent\u id']!=0)://如果它是一个孩子,让我们来获取它的信息 回显“
        • ”; if(isset($menuItem['children'])://如果有孙子,我们也要他们 echo'
            ; outputChildren($menuItem['children']); 回声“
          ”; endif; 回音“
        • ”; 继续; endif; if(isset($menuItem['parent\u id'])&&&$menuItem['parent\u id']==0)://检查它是否是父类别 回音“”; endif; endforeach; 回声“
        ”; } 函数outputChildren($child){ 如果(isset($child['name']): 回显“
      • ”; 如果(!empty($child['children']): 回声“
          ”; 输出儿童($child['children']); 回声“
        ”; endif; 回音“
      • ”; endif; }

        您可以将
        echo
        语句更改为
        $htmm.=

        如果
        打印($arr)
        ,您会得到什么?我应该在哪里打印它?在哪个位置?请在
        构建列表
        函数的开始处尝试。谢谢,但不起作用。现在它缺少了两个链接。请参阅第一篇文章,了解我所做的更改。感谢它丢失的“AllgeMines”,这是“EInstellungen”中的一个子项,以及它丢失的“test1”,这是“AllgeMines”中的一个子项。@user1766080,因为你可以使用“递归”遍历每个“子项”,看到这个答案:@user1766080如果答案不清楚,请告诉我,我会尝试解释谢谢你的答案。我现在读了三遍,但我不明白。看起来很复杂。另一件让我难以理解的事情是我来自奥地利。所以我的英语不是最好的。如果你能帮我,那就太好了。谢谢你抽出时间。