计算帖子数量使用每个标签
因此,我正在研究一个自定义博客系统,每篇文章中,我都会显示与该帖子相关的标签列表。我想在每个标签中旁边的每个标签旁边都有一个数量的数量,即其他每个标签使用了多少帖子,这就是我在弄清楚的问题。
在这里,我有一张帖子,标签,然后是post_tags的3个表,用于分配哪些帖子具有标签。
mysql> DESC post;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | mediumint(9) | NO | PRI | NULL | auto_increment |
| contents | longtext | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
mysql> DESC tags;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | mediumint(9) | NO | PRI | NULL | auto_increment |
| tag | varchar(255) | NO | | NULL | |
+-------+--------------+------+-----+---------+----------------+
mysql> DESC post_tags;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | mediumint(9) | NO | PRI | NULL | auto_increment |
| post_id | mediumint(9) | NO | | NULL | |
| tag_id | mediumint(9) | NO | | NULL | |
+---------+--------------+------+-----+---------+----------------+
这是我用来提取帖子标签列表的代码
$stmt = $con->prepare('SELECT * FROM tags LEFT JOIN post_tags ON post_tags.tag_id = tags.id JOIN post ON post.id = post_tags.post_id WHERE post.id = ?');
$stmt->bind_param('i', $_GET['id']);
$stmt->execute();
$tags = $stmt->get_result();
,以及如何显示它们
<?php while ($tag = $tags->fetch_assoc()) : ?>
<span class="tags"><a href="tags.php?<?= $tag['tag'] ?>" class="tag tag-35762 "><span class="name"><?= $tag['tag'] ?></span><span class="count">1</span></a>
<? endwhile ?>
So I am working on a custom blog system where on each post I show the list of tags associated with that post. Next to each one of the the tags I want to show a count of how many other posts use each tag, and that is what I am having issue figuring out.
Here the the 3 tables I have one for the posts, tags, and then post_tags for assigning which posts have what tags.
mysql> DESC post;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | mediumint(9) | NO | PRI | NULL | auto_increment |
| contents | longtext | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
mysql> DESC tags;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | mediumint(9) | NO | PRI | NULL | auto_increment |
| tag | varchar(255) | NO | | NULL | |
+-------+--------------+------+-----+---------+----------------+
mysql> DESC post_tags;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | mediumint(9) | NO | PRI | NULL | auto_increment |
| post_id | mediumint(9) | NO | | NULL | |
| tag_id | mediumint(9) | NO | | NULL | |
+---------+--------------+------+-----+---------+----------------+
Here is the code I use to pull the list of tags for the post
$stmt = $con->prepare('SELECT * FROM tags LEFT JOIN post_tags ON post_tags.tag_id = tags.id JOIN post ON post.id = post_tags.post_id WHERE post.id = ?');
$stmt->bind_param('i', $_GET['id']);
$stmt->execute();
$tags = $stmt->get_result();
and how I display them
<?php while ($tag = $tags->fetch_assoc()) : ?>
<span class="tags"><a href="tags.php?<?= $tag['tag'] ?>" class="tag tag-35762 "><span class="name"><?= $tag['tag'] ?></span><span class="count">1</span></a>
<? endwhile ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样的东西?
或者
Something like this?
OR