改进PHP中的注释结构

时间:2014-02-21 14:34:39

标签: php mysql sorting comments

我在PHP中有一个半工作的评论结构,我希望改进它,以便回复以正确的方式显示,无论他们回复什么评论。

我将在下面展示的sort_comments函数是需要改进的。

目前,我的评论数组结构采用以下格式:

Array
(
    [0] => Array
        (
            [comment_id] => e465ce0a5301b8ed2eb66be06f768184f7727e3a
            [profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
            [name] => Chris Moore
            [parent_id] => 
            [comment] => You can do that, easy peasy!
            [type] => a
            [ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
            [registered] => 2013-11-19 14:34:41
            [child] => Array
                (
                    [0] => Array
                        (
                            [comment_id] => 68911c41a8cb13742dfd16f299aa3a2c9e87e16d
                            [profile_id] => 1dd36ac747735a3ee8a1d47750e1515ab7ac0d53
                            [name] => James Boyd
                            [parent_id] => e465ce0a5301b8ed2eb66be06f768184f7727e3a
                            [comment] => hello chris
                            [type] => a
                            [ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
                            [registered] => 2013-11-27 15:40:31
                        )

                    [1] => Array
                        (
                            [comment_id] => 7252cdab2c50dbb028e7b41f04bfb3fa7f6ff39d
                            [profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
                            [name] => Chris Moore
                            [parent_id] => e465ce0a5301b8ed2eb66be06f768184f7727e3a
                            [comment] => Test 14:17
                            [type] => a
                            [ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
                            [registered] => 2014-02-21 14:17:10
                        )

                    [2] => Array
                        (
                            [comment_id] => 3b221cd10ca36f7b723f19d6f53d87e42878280c
                            [profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
                            [name] => Chris Moore
                            [parent_id] => e465ce0a5301b8ed2eb66be06f768184f7727e3a
                            [comment] => Testing 14:23
                            [type] => a
                            [ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
                            [registered] => 2014-02-21 14:23:44
                        )

                )

        )

    [1] => Array
        (
            [comment_id] => b94ce651fc010917e6b18fa6677c0941bacdcab0
            [profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
            [name] => Chris Moore
            [parent_id] => 
            [comment] => Testing
            [type] => a
            [ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
            [registered] => 2014-02-20 16:11:38
        )
)

如您所见,我使用附加原始数组的 [child]数组,请参阅数组索引1,以创建回复结构

我的职能:

function sort_comments($ar){

    $comments = array();

    foreach($ar as $item){
        if(empty($item['parent_id'])){
            $comments[] = $item;
        }
        else {
            $parent_array = array_search($item['parent_id'],$comments);
            //echo 'Search Array: '.$parent_array;
            if(!is_numeric($parent_array)){
                $comments[$parent_array]['child'][] = $item;
            }
        }
    }

    pA($comments);

    return $comments;
}

function printComment($c){

    $reg = commentDate($c['registered']);
    $img = doesProfilePictureExistTwo($c['profile_id']);
    ?><div class="post-thumb">
        <img src="<?=$img;?>" alt="comment_picture" />
    </div>
    <div class="post-content">
        <p>
            <a href="/x/view/<?=$c['profile_id'];?>/"><?=$c['name'];?></a>
            <time datetime="<?=$c['registered'];?>" class="x_comment_time"><?=$reg;?></time>
        </p>
        <p><?=$c['comment'];?></p>
        <ul class="x_comment_bottom_nav">
            <li><a href="/x/view/comment/<?=$c['comment_id'];?>">permalink</a></li>
            <li><a class="c_reply" href="#" data-id="<?=$c['comment_id'];?>">reply</a></li>
        </ul>
        <form id="<?=$c['comment_id'];?>" class="x_comments_form" method="post" action="/x/view/ambition/<?=$c['ambition_id'];?>/" style="display:none;">
            <input type="hidden" name="action" value="x.add.ambition.comment"/>
            <input type="hidden" name="x_parent_id" value="<?=$c['comment_id'];?>"/>
            <textarea class="x_comment_reply_text_area" name="x_ambition_comment"></textarea>
            <button class="btn">Add</button>
            <button class="btn cancel_comment">Cancel</button>
        </form>
    </div><?
}

function parentComments($comments){
    if(is_array($comments)){
        foreach ($comments as $c){?>
            <div class="post-container"><? 
                printComment($c); 
                childComments($c);
            ?></div>
        <?}
    }
}

function childComments($c){
    if(array_key_exists("child",$c)){
        foreach ($c['child'] as $child){?>
            <div class="post-container-2">
                <? printComment($child); 
                childComments($child); ?>
            </div>
        <?}
    }
}

function getComments($db, $type, $t, $n, $id){

    $query =    "   SELECT c.id AS comment_id, u.id AS profile_id, u.name AS name, c.parent_id, c.comment, '%s' AS type, '%s' AS ambition_id, c.registered 
                    FROM %s c 
                    LEFT JOIN x_user u 
                    ON u.id = c.profile_id
                    WHERE c.%s = '%s'
                    ORDER BY c.registered
                ";

    $query = sprintf($query, $type, $id, $t, $n, $id);
    $result = $db->query($query) or die(mysql_error());
    if($result){
        $rows = $db->rows($result);
        if($rows){
            $comments = array();
            for($i=0;$i<$rows;$i++){
                $comments[] = $db->fetchArray($result);
            }

            $comments = sort_comments($comments);
            return $comments;
        }
    }
}

目前,如果我回复一个回复,评论,子数组中的注释输出,很奇怪?

这是上面提供的代码的当前输出:

enter image description here

任何人都可以帮忙解决问题吗?

由于

0 个答案:

没有答案