使用PHP显示嵌套注释的递归

时间:2015-07-05 19:14:23

标签: php mysql recursion

如何使下面的代码做同样的事情,但没有不必要的重复?假设我只想显示对顶级评论的10个回复(然后允许他们在另一个页面上继续回复)

在另一个论坛上,有人提到你可以从单个数据库查询或某种递归函数构建它们。这就是我提出的(有限的编程知识),但据说效率低下:

<?php

// query to get top-level comments, put each row in an array

foreach ($comments as $comment) {
    // set variables (such as submitter, date, comment text)
?>

    HTML using variables, without the last </div> tag

    <?php

    // query to get level-1 replies, put each in an array

    foreach ($replies1 as $reply1) {

        // set variables (such as submitter, date, comment text)
    ?>

        HTML using variables without last </div> tag

    <?php 

     // continue more foreach loops, the last having its </div> tag

     ?>

    <?php } $replies1 = array(); // reset ?>

    </div> (to end the container the top-level is in)

<?php } ?>

0 个答案:

没有答案