如何使用递归函数嵌套对注释的回复

时间:2016-02-07 19:55:23

标签: php

所以,我有一个父母'在评论表中的列,我调用这样的评论......

SELECT 
    id, 
    parent_id, 
    member, 
    name, 
    email, 
    ip, 
    comment, 
    img, 
    date 
FROM cs_comments 
WHERE (row_id='$cmtid' AND page_type='$cmtt') 
ORDER BY date

我这样称呼函数...... `

$comment_data = array();

while ($comments_array = $comments->fetch_object()) {
        $comment_data[] = $comments_array;
}

echoComments($comment_data, $memberlevel);

我有一个功能设置来打印评论但是,如何打印回复?该功能在其自身内部。

如果我的问题不明确,我可以添加更多信息。

2 个答案:

答案 0 :(得分:2)

首先,您应该考虑直接在SQL查询中使用用户输入数据而不对其进行消毒,这是非常危险的,不建议使用!

2)最好将parent_id的默认值设置为0,我不确定null是否会得到相同的结果

3)你可以在你的mysql查询中转义日期和名称列,它可能与mysql本地变量&功能

<?php
//Code in view
$comments = getComments($cmtid,$page_type);
echoComments($comment);



//This method will take all Comments with their replies at once;
function getComments($cmtid,$page_type,$parent=0)
{
     GLOBAL $mysqli;
     $comments = $mysqli->query("SELECT `id`,parent_id,member,`name`,email,ip,`comment`,img,`date` 
                                FROM cs_comments 
                                WHERE (row_id='{$cmtid}' AND page_type='{$cmtt}' AND parent_id = {$parent}) 
                                ORDER BY date(format)");
     $comment_data = array();
     while($comments_array = $comments->fetch_object())
     {
        $comments_array->replies = $comments_array->parent_id ? getComments($cmtid,$page_type,$comments_array->parent_id) : array();
        $comment_data[] = $comments_array;
     }
     return $comment_data;
}


//This method will print comment with replies
function echoComments($comment_data)
{
    foreach ($comment_data as $comment):
        echo    '<div class="comment">
                    '.$comment->comment.'
                    <hr />
                    '.echoComments($comment->replies).'
                </div>';
    endforeach;
}

?>

您可以使用此样式表为嵌套回复提供一些缩进:

<style>
.comment{
    background:#fff;
}
.comment .comment{
    margin-left:5px;
    background:#eee;
}
<style>

答案 1 :(得分:1)

由于你的问题非常抽象,所以这就是我开始的解决方案:

// call it somewhere else inside some other function
$this->recursiveCaller();

// now the function itself
function recursiveCaller($parent = 0, $indent = 0) {
    // $Database->select is just pseudo for however you do the query!!!
    $Database->select('SELECT id, parent_id, member, name, email, ip, comment, img, date FROM cs_comments WHERE (row_id='.$cmtid.' AND page_type='.$cmtt.' AND parent_id = '.$parent.') ORDER BY date');
    $menuItems = $this->Database->fetchAll();

    while($comments_array = $comments->fetch_object()) {
    ?>
        <div style="margin-left: <?= $indent ?>px;">
        <!-- INSERT CONTENT HERE --> 
        </div>
        <?php
        // $this assumes you are doing this inside a class
        // it also assumes $comment_array holds an object retrieved by the query, where $comments_array->parent_id points to the relevant parent_id column
        $this->recursiveCaller($comments_array->parent_id, $indent+50);
    }
}

它将从打印没有缩进的“基础”图层开始,并且每次向下递归时都会创建一个“{”}“50px”左边距。