【问题标题】:Adding and showing tags added to a post using php使用 php 添加和显示添加到帖子的标签
【发布时间】:2014-04-20 16:20:57
【问题描述】:

我在一个社交新闻网站上工作,用户发布任何内容,网站为用户提供标签选项,标签可以是“政治”或“娱乐”任何取决于用户的东西,他们可以选择多个标签。 .. 所以我的问题是如何将帖子的标签存储在特定帖子具有各自标签的数据库中,以及如何在选择特定标签时从数据库中获取所有帖子...... 和这个 stackoverflow 网站一样

帮帮我....

【问题讨论】:

    标签: php database tags


    【解决方案1】:

    这是一个很大的问题,但让我们尝试一下 :-)

    数据库: 这是一个 N:N 关系,所以你需要 3 个表 1) 有 id 的帖子 2) 有 id 的标签 3) 带有 post_id 和 tags_id 的 posts_tags 这将使得一个帖子可以有多个标签

    如何储存: 创建为您提供 post_id 的帖子,然后通过将这个新创建的带有指定 post_ids 的 post_id 插入到 posts_tags 表中来添加标签

    如何根据标签检索: select * from posts_tags where tags_id = x;

    【讨论】:

      【解决方案2】:

      你已经有一张桌子供你发帖,例如

      CREATE TABLE IF NOT EXISTS post (
        post_id int(11) NOT NULL,
        -- more columns as needed
        PRIMARY KEY (post_id)
      );
      

      你应该有一个有效标签的表格,例如

      CREATE TABLE IF NOT EXISTS tag (
        tag_id varchar(32) NOT NULL,
        PRIMARY KEY (tag_id)
      );
      

      添加您的有效标签

      INSERT INTO tag (tag_id) VALUES ('Entertainment'), ('Politics');
      

      最后,您需要在帖子和标签之间建立多对多关系

      CREATE TABLE IF NOT EXISTS tag_to_post (
        tag_to_post_id int(11) NOT NULL AUTO_INCREMENT,
        tag_fk varchar(32) NOT NULL,
        post_fk int(11) NOT NULL,
        PRIMARY KEY (tag_to_post_id),
        FOREIGN KEY (tag_fk) REFERENCES tag(tag_id),
        FOREIGN KEY (post_fk) REFERENCES post(post_id)
      );
      

      【讨论】:

        猜你喜欢
        • 2020-04-27
        • 1970-01-01
        • 1970-01-01
        • 2014-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多