php html显示分层数据

发布于 2024-11-04 06:05:30 字数 411 浏览 7 评论 0原文

我有一个数组 ($title, $deep)

$title($depth)
////////////////////////////////////
ELECTRONICS(0)
    TELEVISIONS(1)
        TUBE(2)
        LCD(2)
        PLASMA(2)
    PORTABLE ELECTRONICS(1)
        MP3 PLAYERS(2)
            FLASH(3)
        CD PLAYERS(2)
        2 WAY RADIOS(2)
//////////////////////

我怎样才能用

  • 显示这个结构

I have an array ($title, $depth)

$title($depth)
////////////////////////////////////
ELECTRONICS(0)
    TELEVISIONS(1)
        TUBE(2)
        LCD(2)
        PLASMA(2)
    PORTABLE ELECTRONICS(1)
        MP3 PLAYERS(2)
            FLASH(3)
        CD PLAYERS(2)
        2 WAY RADIOS(2)
//////////////////////

How could I display this structure with <ul><li>

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

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

发布评论

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

评论(2

梦开始←不甜 2024-11-11 06:05:30

它的基础知识...跟踪深度,并打印出

标签以将深度推向当前深度。请记住,HTML 不需要 标签,它让生活变得更轻松。您可以在每个项目之前打印一个

  • ,然后让元素根据需要自行关闭。
  • 现在,至于检查列表的具体细节,这取决于结构(在编辑时,您并不想分享该结构)。不过,我可以想到两种明智的方法来构建这样的列表。

    $depth = -1;
    // May be foreach($arr as $title => $itemDepth), depending on the structure
    foreach ($arr as $item)
    {
        // if you did the 'other' foreach, get rid of this
        list($title, $itemDepth) = $item;
    
        // Note, this only works decently if the depth increases by
        // at most one level each time.  The code won't work if you
        // suddenly jump from 1 to 5 (the intervening <li>s won't be
        // generated), so there's no sense in pretending to cover that
        // case with a `while` or `str_repeat`.
        if ($depth < $itemDepth)
            echo '<ul>';
        elseif ($depth > $itemDepth)
            echo str_repeat('</ul>', $depth - $itemDepth);
    
        echo '<li>', htmlentities($title);
        $depth = $itemDepth;
    }
    
    echo str_repeat('</ul>', $depth + 1);
    

    这不会生成有效的 XHTML。但大多数人无论如何都不应该使用 XHTML。

    The basics of it...keep track of the depth, and print out <ul> and </ul> tags to nudge the depth toward the current depth. Keep in mind that HTML does not require </li> tags, and it makes life easier. You can just print out a <li> before each item, and let the elements close themselves as needed.

    Now, as for the specifics of going over your list, it depends on the structure (which, at the time of this edit, you haven't cared to share). There are two sensible ways i can think of to structure such a list, though.

    $depth = -1;
    // May be foreach($arr as $title => $itemDepth), depending on the structure
    foreach ($arr as $item)
    {
        // if you did the 'other' foreach, get rid of this
        list($title, $itemDepth) = $item;
    
        // Note, this only works decently if the depth increases by
        // at most one level each time.  The code won't work if you
        // suddenly jump from 1 to 5 (the intervening <li>s won't be
        // generated), so there's no sense in pretending to cover that
        // case with a `while` or `str_repeat`.
        if ($depth < $itemDepth)
            echo '<ul>';
        elseif ($depth > $itemDepth)
            echo str_repeat('</ul>', $depth - $itemDepth);
    
        echo '<li>', htmlentities($title);
        $depth = $itemDepth;
    }
    
    echo str_repeat('</ul>', $depth + 1);
    

    This won't generate valid XHTML. But most people shouldn't be using XHTML anyway.

    各自安好 2024-11-11 06:05:30

    您可以使用这样的递归函数。

    $data = array(
        'electronics' => array(
            'televisions' => array(
                'tube',
                'lcd',
                'plasma',
            ),
            'portable electronics' => array(
                'MP3 players' => array(
                    'flash',
                ),
                'CD players',
                '2 way radios',
            ),
        ),
    );
    
    function build_ul($contents){
        $list = "<ul>\n";
    
        foreach($contents as $index => $value){
            if(is_array($value)){
                $item = "$index\n" . build_ul($value);
            } else {
                $item = $value;
            }
           $list .= "\t<li>$item</li>\n";
        }
    
        $list .= "</ul>\n";
    
        return $list;
    }
    
    
    print build_ul($data);
    

    您必须修改该函数才能添加显示该类别总计的数字。

    请注意,由于 PHP 没有像其他语言(例如 Lisp)那样针对处理递归函数进行优化,因此如果您有大量数据,您可能会遇到性能问题。另一方面,如果您的层次结构比三级或四级更深,那么您无论如何都会开始遇到问题,因为很难在单个网页中合理地显示那么多层次结构。

    You could use a recursive function like this.

    $data = array(
        'electronics' => array(
            'televisions' => array(
                'tube',
                'lcd',
                'plasma',
            ),
            'portable electronics' => array(
                'MP3 players' => array(
                    'flash',
                ),
                'CD players',
                '2 way radios',
            ),
        ),
    );
    
    function build_ul($contents){
        $list = "<ul>\n";
    
        foreach($contents as $index => $value){
            if(is_array($value)){
                $item = "$index\n" . build_ul($value);
            } else {
                $item = $value;
            }
           $list .= "\t<li>$item</li>\n";
        }
    
        $list .= "</ul>\n";
    
        return $list;
    }
    
    
    print build_ul($data);
    

    You'll have to modify the function in order to add the numbers showing totals for the category.

    Note that because PHP is not optimized for handling recursive functions like some other languages (e.g. Lisp), you may run into performance problems if you have large amounts of data. On the other hand, if you've got a hierarchical structure deeper than three or maybe four levels, you're going to start running to problems anyway because it's hard to display that much hierarchy sensibly in a single web page.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文