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

标签 php mysql sql view

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

我的尝试:

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 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/>';
            }

        }

关于php - 在 PHP 中显示和链接外键内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32023241/

相关文章:

php - 数据为 UTF-8,Ajax 错误返回一些字符

mysql - 按天对 Unix 时间戳进行分组,产生不均匀间隔的组

SQL:基于聚合函数过滤组

php - 无法使用 PHP 5.5 将数据添加到 webmatrix 3 中的 MySQL 5.7

php - PHP 函数调用开销有多重要?

php - Elasticsearch按查询删除多个术语

php - 获取一天的总工作时间、总工作时间、总外出时间 mysql

javascript - 如何使 href 在 jquery-bootgrid 基本插件中工作

php - 在 for 循环中按字段值返回最常见的结果?

mysql - 将单元格的值设置为等于另一行中单元格的值