简体   繁体   English

PHP / MySQL如何使我的评论和回复正确显示?

[英]PHP/MySQL How to make my comments and replies display correctly?

I'm trying to display my comments and my comments replies on my web page but I can't get the replies to display with the correct comment can some one help correct this problem? 我试图在网页上显示我的评论和评论回复,但是我无法显示带有正确评论的回复,有人可以帮助解决此问题吗?

And by the way all the if else statements are to display different colors for the commenter or replier if he or she is the page creator or user. 顺便说一句,如果评论者或回复者是页面创建者或用户, if else所有if else语句都将显示不同的颜色。

Here is the MySQL tables. 这是MySQL表。

CREATE TABLE comments (
comment_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
parent_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
page_id INT UNSIGNED NOT NULL,
comment TEXT NOT NULL,
date_created DATETIME NOT NULL,
PRIMARY KEY (id),
KEY user_id (user_id),
KEY page_id (page_id)
);


CREATE TABLE users (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL, 
PRIMARY KEY (id)
);

Here is the PHP & MySQL code. 这是PHP和MySQL代码。

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT comments.*, users.*
                             FROM comments
                             LEFT JOIN users 
                             ON comments.user_id = users.user_id
                             WHERE page_id = $page_id
                             ORDER BY parent_id ASC");

if (!$dbc) {
    print mysqli_error();
}  else {
    while($row = mysqli_fetch_array($dbc)){
        $comment_user = $row['user_id'];
        $comment_id = $row['comment_id'];
        $comments = $row['comment'];
        $parent_id = $row['parent_id'];

            if($comment_user == $user_id && $parent_id == 0){

                echo '<p class="page-creator-comment">' . $comments . '</p>';

            } else if($comment_user != $user_id && $parent_id == 0) {

                echo '<p class="commenter">' . $comments . '';

            } else if($comment_user == $user_id && $parent_id >= 1){

                echo '<p class="page-creator-reply">' . $comments . '</p>';

            } else if($parent_id >= 1){

                echo '<p class="reply">' . $comments . '</p>';

            }
    }
}

You could pull the comments into an associative array, determine if they are a parent, then attach children to the parent. 您可以将注释拉入关联数组,确定它们是否是父项,然后将子项附加到父项。 After you have sorted/matched your comments, loop through again and spit them out. 在您对注释进行排序/匹配之后,再次循环浏览并吐出。

Something very roughly like: 很像的东西:

$comments = array();
if($parent_id == 0)
{
  $comments[] = array($comment_id => [all your comment data (possibly as stdObject)],array());
}
else if($parent_id  > 0)
{
  $comments[$parent_id][] => array($comment_id => [all your comment data]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM