简体   繁体   English

PHP-MySQL标记

[英]PHP-MySQL Tagging

I have a comics website which currently allows users to choose which comics they view by category: Charts, Office, Life, Misc, etc. 我有一个漫画网站,目前允许用户选择按类别查看的漫画:图表,办公室,生活,杂项等。

I'd like to implement a tagging system, similar to what we have here on StackOverflow, which will describe more of the content of each comic rather than its category. 我想实现一个标记系统,类似于我们在StackOverflow上的内容,它将描述每个漫画的更多内容而不是其类别。 Ex: In Charts category, I have several business related... 例如:在图表类别中,我有几个与业务相关的...

My simple solution would be to handle it just how I've handled my categorization- 我的简单解决方案就是如何处理我的分类 -

  1. Create a "Tags" table with tagid, tagname, tagdescription 使用tagid,tagname,tagdescription创建“Tags”表
  2. Add a tagid_ForeignKey field in comics table, and add a tag to each post. 在漫画表中添加tagid_ForeignKey字段,并为每个帖子添加标签。
  3. When a user clicks on a tag, it will show only those posts with that tag... or if there is also a category specified, it will show that specific category with that specific tag. 当用户点击代码时,它只会显示带有该代码的帖子...或者如果还指定了某个类别,则会显示该特定代码的特定类别。

This approach, however, seems to limit me to one tag per category. 然而,这种方法似乎限制我每个类别一个标签。 What if I have a comic that is business and relationships related... so It'd need both of those tags. 如果我有与业务和关系有关的漫画该怎么办...所以它需要两个标签。

How would I be able to attach multiple tags per comic? 如何为每个漫画附加多个标签?


EDIT: 编辑:

A few more questions: 还有一些问题:

1) What do I insert into my new relational table... anything? 1)我在新的关系表中插入什么内容......什么?

2) Also, for while ($row = $tag->fetch_assoc()) { , how can I loop through a table if there is a join? 2)另外,对于while ($row = $tag->fetch_assoc()) { ,如果有连接,我如何在表中循环? Isn't that an associative array? 这不是一个关联数组吗?

3) The issue is that I am echoing out the tag choices as such, so once a user clicks on a link, how would you be able to allow them to then click on another link to assign a 2nd tag? 3)问题是我正在回应标签选​​择,所以一旦用户点击链接,你怎么能让他们再点击另一个链接来分配第二个标签?

在此输入图像描述

function getTags() {
include 'dbconnect.php';
global $site;

$tag = $mysqli->query("SELECT tagid, tagname FROM tags");   
//$tag = $mysqli->query("SELECT * FROM comics c INNER JOIN comictags ct ON (c.comicID = ct.comicID) WHERE ct.tag_id IN (1, 2, 3) GROUP BY c.comic_id");

mysqli_close($mysqli);

while ($row = $tag->fetch_assoc()) {        
    echo "<a href='?action=homepage&site=" . $site . "&tag=" . $row['tagid'] . "&tagname=" . $row['tagname'] . "'/>" . $row['tagname'] . "</a><br />";
  }

}

Just add another table. 只需添加另一个表。 Then you have three: One for Tags, one for Comics, and one for the relationship. 然后有三个:一个用于标签,一个用于漫画,以及一个用于关系。 You have to have this indirection table to properly store a many-to-many relationship. 您必须具有此间接表才能正确存储多对多关系。 This allows each comic to have zero or more tags (and vice versa). 这允许每个漫画具有零个或多个标签(反之亦然)。

You can accomplish this with a many-to-many relationship. 您可以通过多对多关系来完成此任务。 A many-to-many relationship uses a relational join table that would look like this: 多对多关系使用的关系联接表如下所示:

+---------------+---------------+
|   comic_id    |     tag_id    |
+---------------+---------------+
|       1       |        2      |
+---------------+---------------+
|       1       |        3      |
+---------------+---------------+
|       1       |        4      |
+---------------+---------------+

Now, in your query: 现在,在您的查询中:

SELECT * FROM comics c INNER JOIN comic_tags ct ON (c.comic_id = ct.comic_id) WHERE ct.tag_id IN (1, 2, 3) GROUP BY c.comic_id

Where 1, 2, 3 are the tags the user selected that they would like to see. 其中1、2、3是用户希望查看的标签。

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

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