在 PHP 中显示和链接外键内容

Displaying and linking Foreign Key content in PHP

我正在参加一些在线课程,在其中一个练习中,我们将为博客创建两个表 - 博客文章和博客文章 - 并通过外键连接它们,然后显示两个表的所有内容。评论应仅链接到特定文章,同时也允许多条评论。

我的尝试:

function list_articles() { 
    include('core/db/db_connection.php');
    $sql = "SELECT blog.title, blog.content, blog.posted_by, blog.date, article_comments.comments, article_comments.comment_by
            FROM blog LEFT OUTER JOIN article_comments
            ON blog.content_id = article_comments.content_id
            WHERE blog.content != ''
            ORDER BY blog.content_id DESC";
    $result = mysqli_query($dbCon, $sql);
    while ($row = mysqli_fetch_array($result)) {
        echo 
            "<h5 class='posted_by'>Posted by " . $posted_by = $row['posted_by'] . " on " . $row['date'] . "</h5>" . 
            "<h1 class='content_headers'>" . $title = $row['title'] . "</h1>" . 
            "<article>" . $content = $row['content'] . "</article>" . 
            "<div class='commented_by'>Posted by: " . $row['comment_by'] . "</div>" . 
            "<div class='comments'>Comments: " . $row['comments'] . "</div>";
    }
}

这就是我在数据库中插入评论的方式:

function insert_comments($comment_by, $comments) {
    include('core/db/db_connection.php');
    $sql =  "SELECT blog.content_id, article_comments.blog_id  
             FROM blog AS blog
             INNER JOIN article_comments AS article_comments ON article_comments.blog_id > blog.content_id";
    mysqli_query($dbCon, $sql);
}

在 PHPMyAdmin 中,外键工作正常,评论链接到特定文章。我想在网页上转置它。当我在页面上插入一篇新文章时它工作正常,但是当我尝试为该文章插入评论时它不会显示它。

如果我将 ON blog.content_id = article_comments.content_id 更改为 ON blog.content_id = article_comments.blog_id(blog_id 是外键的字段名称) - 它会显示一篇文章的所有评论 - 但它会复制该文章对于与之关联的每个评论。这有任何意义吗?我已尽我所能对其进行解释。如果您需要进一步说明,请告诉我。谢谢

顺便说一句,这是我用来创建外键的语句:

ALTER TABLE article_comments ADD CONSTRAINT comment_blog_fk FOREIGN KEY (blog_id) REFERENCES wt.blog(content_id) ON DELETE NO ACTION ON UPDATE CASCADE;

编辑:我用ON blog.content_id = article_comments.blog_id

得到的结果
Article title: LOREM IPSUM
Content: LOREM IPSUM DOLOR SIT AMET....
--------------------------------------
Name: DSK
Comment: Great article!

-- HERE IT DUPLICATES THE ARTICLE TO INSERT A NEW COMMENT --

Article title: LOREM IPSUM
Content: LOREM IPSUM DOLOR SIT AMET....
--------------------------------------
Name: DSK
Comment: Great article! - 2nd comment

如您所见,它会为每条插入的评论复制文章。所以我最终得到了两篇包含不同评论的重复文章。 If I'll have 100 comments, the article will get replicated 100 times

我期望的行为:

Article title: LOREM IPSUM
Content: LOREM IPSUM DOLOR SIT AMET....
-------------------------------------- \ COMMENTS \
Name: DSK
Comment: Great article!
--------------------------------------
Name: DSK
Comment: Great article! - 2nd comment

试试这个:

        $posts = array();
        $pdo = new PDO('mysql:host=localhost;dbname=your_db', 'user', 'password');
        // for example all fields
        $query = $pdo->query('
            SELECT * 
              FROM blog AS blog
             INNER JOIN article_comments AS article_comments ON article_comments.blog_id = blog.content_id
        ');

        while ($row = $query->fetch()) {
            $idContent = $row['content_id'];

            if (!isset($posts[$idContent])) {
                $posts[$idContent] = array(
                    'posted_by' => $row['posted_by'],
                    'title' => $row['title'],
                    'content' => $row['content'],
                    'comments' => array()
                );
            }

            $posts[$idContent]['comments'][] = array(
                'comment_by' => $row['comment_by'],
                'comment' => $row['comment'],
            );

        }

        foreach ($posts as $post) {
            echo '
                Post: ' . $row['title'] . ' . Posted by: ' . $row['posted_by'] .
                '<br/>Content: ' . $row['content'] .
                '<br/>Comments: ';
            ;

            foreach ($post['comments'] as $comment) {
                echo $comment['comment'] . '. Comment by: ' .$row['comment_by'] . '<br/>';
            }

        }