Php 计算从另一个表中获取值并显示在循环中的表的行数
我想从表“posts”中获取post_id,并使用此值计算表“comment_post”中的行,其中相同的列post_id等于表posts中的值 这是我的两张桌子:Php 计算从另一个表中获取值并显示在循环中的表的行数,php,mysql,count,Php,Mysql,Count,我想从表“posts”中获取post_id,并使用此值计算表“comment_post”中的行,其中相同的列post_id等于表posts中的值 这是我的两张桌子: posts id | post_id | title ----+---------+------- 1 | 1 | title1 2 | 2 | title2 3 | 3 | title3 comment\u post id | post_id | comment_id ---+
posts
id | post_id | title
----+---------+-------
1 | 1 | title1
2 | 2 | title2
3 | 3 | title3
comment\u post
id | post_id | comment_id
---+----------+-----------
1 | 1 | 26
2 | 1 | 27
3 | 2 | 28
4 | 1 | 29
5 | 3 | 30
用户评论存储在单独的“评论”表中,帖子发布在表posts
中。
我在两个表之间建立了一个关系,并将该表称为“comment_post”
我设置了一个循环来返回浏览器中的帖子
<div class="row">
<?php
$i = 0;
foreach ($posts as $post):{
if(++$i > 11) break;
}
?>
<!-- post -->
<div class="post col-xl-6">
<div class="post-thumbnail"><a href="single_post.php?post-slug=<?php echo $post['slug']; ?>"><img loading="lazy" src="<?php echo BASE_URL . '/static/images/' . $post['image']; ?>" alt="pcr test" class="img-fluid"></a></div>
<div class="post-details">
<div class="post-meta d-flex justify-content-between">
<div class="date meta-last"><?php echo date("F j, Y ", strtotime($post["created_at"])); ?></div>
<?php if (isset($post['topic']['name'])): ?>
<div class="category">
<a href="<?php echo BASE_URL . 'filtered_posts.php?topic=' . $post['topic']['id'] ?>"><?php echo $post['topic']['name'] ?></a>
</div>
<?php endif ?>
</div>
<a href="single_post.php?post-slug=<?php echo $post['slug']; ?>">
<h3 class="h4"><?php echo $post['title'] ?></h3></a>
<p class="text-muted"><?php echo $post['lead'] ?></p>
<footer class="post-footer d-flex align-items-center"><a href="#" class="author d-flex align-items-center flex-wrap">
<div class="avatar"><img loading="lazy" src="static/images/avatar.jpg" alt="Jamie" class="img-fluid"></div>
<div class="title"><span>Jamie Aldus</span></div></a>
<div class="date"><i class="icon-clock"></i><?php echo date("j-m-y ", strtotime($post["created_at"])); ?></div>
<div class="comments meta-last"><i class="icon-comment"></i></div>
</footer>
</div>
<div class="post-line">
</div>
</div>
<?php endforeach ?>
</div>
通过使用左连接和一些分组更新SQL查询,您可以在函数内部获得评论帖子计数:
function getPublishedPosts($mysqli, $limit = 10) {
$sql = "
SELECT
posts.*, COUNT(1) as commentCount
FROM
posts
LEFT JOIN
comment_posts ON posts.id = comment_posts.post_id
WHERE
posts.published = true
GROUP BY
comment_posts.post_id, posts.id
ORDER BY
posts.created_at DESC
LIMIT ?
";
$query = $mysqli->prepare($sql);
$query->bind_param('i', $limit);
$query->execute();
$posts = $query->get_result();
$final_posts = array();
while($post = $posts->fetch_assoc()) {
$post['topic'] = getPostTopic($post['id']);
$final_posts[$post['id']] = $post;
}
return $final_posts;
}
注释
global$conn代码>在调用函数时将$conn
传入(很像将DB链接传递到mysqli.*
)。现在,您可以使用如下函数:
$posts = getPublishedPosts($conn);
mysqli
it,改用PDO
)
commentCount
字段中访问每篇文章的评论数
$limit
参数(默认值为10
);因为在for each循环中有一个限制(现在可以删除)
$final_posts
更改为post_id
上的索引。在这种情况下,这也许不是绝对必要的,但可以让事情变得更容易
for
循环更改为,而:它基本上做相同的事情
getPostTopic
(很可能是另一个JOIN
)
您可以发布想要的结果吗?我不确定您尝试了什么,但大概不会:
从comment\u post中选择COUNT(*),其中post\u id=?
(假设您希望为循环的每个迭代运行一个查询)(或者使用GROUP BY
并获取所有,如果您希望使用一个查询),我不知道是否理解得很好,但是类似的东西应该可以做到这一点:SELECT p.post\u id,COUNT(c.id)FROM posts p internal JOIN comment\u post c ON c.post\u id=p.post\u id GROUP BY p.post\u id
。我需要从表“posts”中获取post\u id,以便在循环中运行SELECT COUNT(*)查询。您需要显示用于生成结果的代码。。。(即,$posts
来自哪里?)
$posts = getPublishedPosts($conn);
$posts = getPublishedPosts($conn, 20); // Adding the second parameter changes the number of results returned, in this case 20 will be returned