1

看来我被我的递归函数困住了。我在关闭未命名列表 ( </ul>) 和列表项 ( </li>)时遇到问题

我得到的是

 -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;
}

有人对这种事情有更多的经验吗?欢迎所有帮助。

4

1 回答 1

3

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

这部分只是为了测试,它制作了一个三维数组进行测试:

for ($x = 0; $x < 2; $x++) {
    $result["c$x"] = "ROOT-{$x}";
    for ($y = 0; $y < 3; $y++) {
        $result[$x]["c$y"] = "SECOND-{$x}-{$y}";
        $rnd_count1 = rand(0,3);
        for ($z = 0; $z < $rnd_count1; $z++) {
            $result[$x][$y]["c$z"] = "RND-{$x}-{$y}-{$z}";
            $rnd_count2 = rand(0,4);
            for ($c = 0; $c < $rnd_count2; $c++) {
                $result[$x][$y][$z][$c] = "LAST-{$x}-{$y}-{$z}-{$c}";
            }
        }
    }
}
// $result is now four dimensional array with some values
// Last two levels gets random count starting from 0 items.

更新:

添加了一些随机性和第四级来测试数组。

这是将数组排序为无序列表的函数:

function recursive(array $array, $list_open = false){
    foreach ($array as $item) {
        if (is_array($item)) {
            $html .= "<ul>\n";
            $html .= recursive($item, true);
            $html .= "</ul>\n";
            $list_open = false;
        } else {
            if (!$list_open) {
                $html .= "<ul>\n";
                $list_open = true;
            }
            $html .= "\t<li>$item</li>\n";
        }
    }
    if ($list_open) $html .= "</ul>\n";
    return $html;
}


// Then test run, output results to page:
echo recursive($result);

更新:

现在它应该<ul>正确地打开和关闭标签。

于 2012-04-23T22:23:29.753 回答