Php 递归函数-树视图-<;ul>&书信电报;李。。。(卡住了)

Php 递归函数-树视图-<;ul>&书信电报;李。。。(卡住了),php,list,recursion,html-lists,Php,List,Recursion,Html Lists,似乎我一直在使用递归函数。 我在关闭未命名列表()和列表项()时遇到问题 我得到的是 我想要的是: 这是我正在使用的代码 $html.=''; $i=0; foreach($result作为$item) { $html.=“$item->id”; $html.=getSubjects($item->id,NULL,“,$i);join('course\u general\u info','g','su.general\u info\u id=g.id'); //如果他的父母是一个章节-获取章节

似乎我一直在使用递归函数。 我在关闭未命名列表(
)和列表项(
)时遇到问题

我得到的是

我想要的是:

这是我正在使用的代码

$html.='
    '; $i=0; foreach($result作为$item) { $html.=“
  • $item->id”; $html.=getSubjects($item->id,NULL,“,$i);join('course\u general\u info','g','su.general\u info\u id=g.id'); //如果他的父母是一个章节-获取章节id=。。。 如果($chapter_id!=NULL){ $query ->字段('g',数组('short\u title','general\u id')) ->字段('su',数组('id')) ->条件('su.chapter_id',$chapter_id',='); $result=$query->execute(); } //如果父母是受试者— //get value主题id=。。。 否则{ $query ->字段('g',数组('short\u title','general\u id')) ->字段('su',数组('id')) ->条件('su.subject_id',$subject_id',='); $result=$query->execute(); } //因为计数不起作用(drupal) $int=0; foreach(结果为$t){ $int++; } //如果结果中没有值,则返回字符串 如果($int==0){ 返回$string; } 否则{ //创建新的
      $string.=“
        ”; foreach($result作为$item){ //更改id的 $subject\u id=$item->id; $chapter_id=NULL; //并将带函数的字符串-->设置为自己的函数 $string.=“
      • $item->short_title-id-$item->id”; getSubjects(NULL,$subject\u id,$string,$i); $string.=“
      • ”; } $string.=“
      ”; } //我认为这次返回没有必要 返回$string; }
有人对这类事情有更多的经验吗?
欢迎提供所有帮助。

我不确定您想做什么,但这里有一些代码可以测试,看看它是否有助于解决您的问题:

此部分仅用于测试,它制作三维阵列用于测试: ($x=0;$x<2;$x++)的
{
$result[“c$x”]=“ROOT-{$x}”;
对于($y=0;$y<3;$y++){
$result[$x][“c$y”]=“SECOND-{$x}-{$y}”;
$rnd_count1=兰特(0,3);
对于($z=0;$z<$rnd_count1;$z++){
$result[$x][$y][“c$z”]=“RND-{$x}-{$y}-{$z}”;
$rnd_count2=兰特(0,4);
对于($c=0;$c<$rnd_count2;$c++){
$result[$x][$y][$z][$c]=“LAST-{$x}-{$y}-{$z}-{$c}”;
}
}
}
}
//$result现在是具有某些值的四维数组
//最后两级从0项开始随机计数。
更新: 为测试阵列添加了一些随机性和第四级

下面是将数组排序到无序列表的函数: 函数递归(数组$array,$list\u open=false){ foreach($数组作为$项){ if(is_数组($item)){ $html.=“
    \n”; $html.=递归($item,true); $html.=“
\n”; $list_open=false; }否则{ 如果(!$list\u打开){ $html.=“
    \n”; $list_open=true; } $html.=“\t
  • $item
  • \n”; } } 如果($list\u open)$html.=“
\n”; 返回$html; } //然后测试运行,将结果输出到页面: 回声递归(结果); 更新:
现在,它应该正确地打开和关闭
标签。

里面是什么
$result
,如果您可以包含
变量转储($result)的输出,这将非常有用。什么是
$item
,似乎数组
$result
中的每个
$item
都是某种对象,但是什么对象呢?在你们的问题中,你们能包括定义
$item
构造函数的类吗?你们的项在列表中的顺序正确吗?我想要的是:?对我来说,它们的顺序可能是错误的,你能检查并确认这个列表吗?大约
-c33-c2a
@Sampo-->在$Result中,您会发现id、general id和短标题的值。这不完全是问题所在,但它对我帮助很大,这也很有效。非常感谢您修改了您的编辑建议,因为它试图将
    包含在
  • 中,现在所有
    列表项
  • 都正确地包含在
    无序列表
    中。你的建议帮助我做对了,我从来没有发现过。
     -aaa
     -bbb
         -b11
         -b22
         -b33
     -ccc
         -c11
         -c22
         -c33
     -ddd
         -d11
         -d22
         -d33
     -eee
     -fff
    
     -aaa
     -bbb
         -b11
         -b22
             -b2a
             -b2c
             -b2b
         -b33
     -ccc
         -c11
         -c22
         -c33
             -c2a
             -c2c
                 -c2c1
                 -c2c2
             -c2b
     -ddd
         -d11
         -d22
         -d33
     -eee
     -fff
    
    $html .= '<ul>';   
    $i = 0;     
    foreach ($result as $item) 
    {
        $html .= "<li>$item->id";
    
        $html .= getSubjects($item->id, NULL, "",$i);    <--- start
    
        $html .= "</li>";   
     }   
     $html .= '</ul>';
    
    function getSubjects($chapter_id = NULL, $subject_id = NULL, $string = '', $i = 0 ) {
        $i++;
        // getting the information out of the database
        // Depending of his parent was a chapter or a subject
        $query = db_select('course_subject', 'su');
        //JOIN node with users
        $query->join('course_general_info', 'g', 'su.general_info_id = g.id');
        // If his parent was a chapter - get all the values where chapter id = ...
        if ($chapter_id != NULL) {
            $query
                ->fields('g', array('short_title', 'general_id'))
                ->fields('su', array('id'))
                ->condition('su.chapter_id', $chapter_id, '=');
            $result = $query->execute();
        }
        // if the parent is a subject - 
        // get value all the values where subject id = ...
        else {
            $query
            ->fields('g', array('short_title', 'general_id'))
            ->fields('su', array('id'))
            ->condition('su.subject_id', $subject_id, '=');
            $result = $query->execute();
        }
        // Because count doesn't work (drupal)
        $int = 0;
        foreach ($result as $t) {
            $int++;
        }
        //  if there no values in result - than return the string
        if ($int == 0) {
            return $string;
        }
        else {
            // Creating a new <ul>
            $string .= "<ul>";
            foreach ($result as $item) {
                // change the id's
                $subject_id = $item->id;
                $chapter_id = NULL;
                // and set the string --> with the function to his own function
                $string .= "<li>$item->short_title - id - $item->id ";
                getSubjects(NULL, $subject_id, $string, $i);
                $string .="</li>";
            }
            $string .= "</ul>";
        }
        // I thougt that this return wasn't necessary
        return $string;
    }