簡體   English   中英

遞歸函數 - 樹視圖 - <ul><li> … (卡住)

[英]Recursive function - tree view - <ul> <li> … (stuck)

似乎我堅持我的遞歸功能。 關閉未命名列表( </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;
}

有人對這類事情有更多的經驗嗎? 歡迎所有幫助。

我不確定你要做什么,但這里有一些代碼,你可以測試,看看它是否有助於解決你的問題:

這部分僅用於測試,它為測試制作三維數組:

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>標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM