如何在PHP中迭代嵌套注释?

时间:2015-04-30 21:55:36

标签: php

我有基本的样式,我有创建评论的数据库......但现在我不知道如何获取这些评论并迭代它们。因此,该表包含注释ID,内容,作者ID和注释父ID。

我如何遍历评论和回复以将其打印出来,就像这样:

/**
 * Gets the paths of the activated scenes from the BuildSettings
 * @return {String[]} BuiltinArray of Strings
 */
function GetScenePaths () {
    var scenes = new Array();

    for(var i : int = 0; i < EditorBuildSettings.scenes.Length; i += 1) {

        /**
         * Only add scene, if it is enabled in the build settings.
         */
        if (EditorBuildSettings.scenes[i].enabled) {
            scenes.Push(EditorBuildSettings.scenes[i].path);
        }
    }

    /**
     * Return a builtin array instead of a JavaScript Array
     */
    var builtinArray : String[] = scenes.ToBuiltin(String) as String[];
    return builtinArray;
}

等...

1 个答案:

答案 0 :(得分:1)

你可以使用递归。

看起来应该是这样的。 你有一个名为showReplies($replies);的函数,它呈现评论/回复框。

showReplies($replies){
    if (count($replies) == 0){ return; } // stop if no replies need to be printed out
    foreach ($replies as $reply){
        echo $reply->text.'<br>'; // show reply text
        showReplies($reply->childReplies); // continue showing the child replies
    }
}