dsqve08622 2017-04-26 13:07
浏览 44
已采纳

在PHP中创建无限的嵌套列表

I know, the headline is not very specific, but I dont't know how to title it.
I want to create a list from 1 to 10. All those numbers should be clickable and open a sublist. Again with the numbers from 1-10. Like this:

  • 1
  • 2
    • 1
    • 2
    • 3

and so on... Here is some code, I've got:

<?php
$num = $_GET['zahl'];
$zahlen = array();
while($num != 0){
  $part = $num % 10;
  array_push($zahlen, $part);
  $num = floor($num/10);
}
foreach ($zahlen as $key => $value) {
$runs = $value + 1;
for ($i=1; $i < $runs ; $i++) {
  if ($i == 1) {
    echo $ulon . "
";
  }
  echo $lion . "
";
  echo $a . "ordner.php?zahl=" . $i . $amiddle . $i . $aoff . "
";
  echo $lioff . "
";
}
for ($i=1; $i < 11 ; $i++) {
  if ($i == 1) {
    echo $ulon;
  }
  echo $lion . "
";
  echo $a . "ordner.php?zahl=" . $zahlen[0] . $i . $amiddle . $i . $aoff;
  echo $lioff . "
";
}
echo $uloff;
echo "
" . "</body>";
?>
  • 写回答

2条回答 默认 最新

  • doubang9906 2017-04-26 14:05
    关注

    What you wish to achieve should be fairly straigtforward with Javascript rather than PHP - the following uses PHP only to generate the initial list and subsequent sub-lists are generated using javascript.

    <?php
        echo "<ul id='infinite-menu'>";
        for( $i=1; $i < 11; $i++ ){
            echo "<li>$i</li>";
        }
        echo "</ul>";
    ?>
    
    
    <script>
    
        var children=10;
    
        function newnodes(e){
            if( e.target.childNodes.length==1 ){
                var ul=document.createElement('ul');
                e.target.appendChild(ul);
    
                for( i=1; i < children+1; i++ ){
                    var li=document.createElement('li');
                    li.innerHTML=i;
                    ul.appendChild( li );
                }
            }
        }
        var col=document.querySelectorAll('ul#infinite-menu li');
        if( col )for( n in col )if( col[n].nodeType==1){
            col[n].addEventListener('click',newnodes,false);
        }
    
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀