【问题标题】:How to properly render the same thing as drop down-list, but in selectbox?如何正确呈现与下拉列表相同的内容,但在选择框中?
【发布时间】:2014-05-16 17:56:46
【问题描述】:

有一个数组看起来像这样:

Array
(
    [items] => Array
        (
            [1] => Array
                (
                    [id] => 1
                    [parent_id] => 0
                    [name] => About us
                )

            [3] => Array
                (
                    [id] => 3
                    [parent_id] => 1
                    [name] => Person 2
                )

            [2] => Array
                (
                    [id] => 2
                    [parent_id] => 1
                    [name] => Person 1
                )

            [5] => Array
                (
                    [id] => 5
                    [parent_id] => 1
                    [name] => Gallery
                )

            [4] => Array
                (
                    [id] => 4
                    [parent_id] => 1
                    [name] => My CV
                )

            [6] => Array
                (
                    [id] => 6
                    [parent_id] => 0
                    [name] => Contact us
                )

            [8] => Array
                (
                    [id] => 8
                    [parent_id] => 6
                    [name] => Contactinfo
                )

            [7] => Array
                (
                    [id] => 7
                    [parent_id] => 6
                    [name] => My pictures
                )

        )

    [parents] => Array
        (
            [0] => Array
                (
                    [0] => 1
                    [1] => 6
                )

            [1] => Array
                (
                    [0] => 3
                    [1] => 2
                    [2] => 5
                    [3] => 4
                )

            [6] => Array
                (
                    [0] => 8
                    [1] => 7
                )

        )

)

还有一个我用来为下拉菜单呈现数据的函数:

// I render drop-down menu like this
echo buildMenu(0, $hierarchy)


function buildMenu($parentId, array $menuData) {
    $html = ''; 

    if (isset($menuData['parents'][$parentId])) {

        $html = '<ul>'; 

        foreach ($menuData['parents'][$parentId] as $itemId) { 

            $html .= '<li><a href="#">' . $menuData['items'][$itemId]['name'] . '</a>'; 

            // find childitems recursively 
            $html .= buildMenu($itemId, $menuData); 

            $html .= '</li>'; 
        }

        $html .= '</ul>'; 
    }

    return $html; 
}

这是按预期工作的。但是现在我想把下拉菜单变成一个选择框,这样输出就会像

<select>

  <option value="id1">Parent</option>
  <option value="id2">- Child 1</option>
  <option value="id2">-- Sub-Child 1</option>

</select>

我该怎么做?

【问题讨论】:

  • 请努力自己解决。如果您无法解决它,请返回一个最小的代码示例来说明您遇到的问题。
  • 我需要一个建议至少从哪里开始
  • 我没有说我需要开始学习一门语言。我建议您编写有意义的 cmets
  • 其实我的意思是我写的,以一种友善的方式。我喜欢提供帮助,但这不是寻求帮助,而是寻求解决您甚至没有尽最大努力解决的挑战的方法,这就是为什么我得出结论认为您可能需要学习一些 PHP 基础知识.有了这些,您应该能够以最少的努力自己解决这个问题。现在你得到的只是一个交给你的答案。

标签: php arrays recursion drop-down-menu


【解决方案1】:

通过修改您当前的功能如下

 function buildMenu($parentId, array $menuData) {
     $html = '';
     //static variable, shared across all (recursive in this context) calls of this function
     static $level = 1;

     if(isset($menuData['parents'][$parentId])) {

        if($level == 1)
            $html .= '<select>';

        foreach($menuData['parents'][$parentId] as $itemId) {

           $html .= '<option value="__value_here__">' . str_repeat('--', $level - 1) . $menuData['items'][$itemId]['name'] . '</option>';

           //subsequent items will be indented one level
           $level++;

           $html .= buildMenu($itemId, $menuData);

           //recursive call has returned, so restore level
           $level--;
        }

        if($level == 1)
           $html .= '</select>';
     }

     return $html;
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    相关资源
    最近更新 更多