duanqianhuan3994 2014-02-05 23:38
浏览 30

如何使用每个行级属性渲染金字塔?

array (

   array(

     ['name'] => 'test1',
     ['level'] => 1

   },
   array(

     ['name'] => 'test2',
     ['level'] => 2

   },

   array(

     ['name'] => 'test3',
     ['level'] => 2

   },
   array(

     ['name'] => 'test4',
     ['level'] => 3

   },
   array(

     ['name'] => 'test5',
     ['level'] => 3

   },
   array(

     ['name'] => 'test6',
     ['level'] => 3

   },
   array(

     ['name'] => 'test7',
     ['level'] => 3

   },



)

I have an array of arrays sorted by level, thus I want to make something like this:

                  ________  
                 |        | 
                 |  test1 | 
                 |________| 
       ________              ________
      |        |            |        |
      |  test2 |            |  test3 |
      |________|            |________|
 ________   ________   ________   ________
|        | |        | |        | |        |
|  test4 | |  test5 | |  test6 | |  test7 |
|________| |________| |________| |________|

render them like a pyramide each row by level, but first how do I render it by using recursion or using a while loop? to make each level wrapped by <ul> and the nodes by <li>

I'am trying to render it by loop, but I don't know how, what is the logic?:

    while ($item = $this->dbtree->NextRow()) {

        $rows[] = (object)$item;

    }
    print_r($res); // list of arrays like the above
    exit;

thanks for attention

  • 写回答

1条回答 默认 最新

  • duanjizi9443 2014-02-05 23:47
    关注

    You can first iterate over the array and sort them in to the levels in your application logic:

    foreach ($res as $resValue){
        if (!isset($levelSorted[$resValue['level'])){
            $levelSorted[$resValue['level'] = array();
        }
        $levelSorted[$resValue['level'][] = $resValue;
    }
    

    In your view you can now do something like this:

    foreach ($levelSorted as $level => $levelRes){
        echo '<ul id="level' . $level . '">';
        foreach ($levelRes as $singleRes){
            echo '<li>' . $singleRes['name'] . '</li>';
        }
        echo '</ul>';
    }
    

    At the end, you have more than one loop, but it would be consistent with the MVC pattern.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!