简体   繁体   中英

how can i create a tagging system using php and mysql?

wondering how i can create a tagging system in php and with a mysql database, my initial thoughts were to create a row in the table where the articles are stored, named tags, and list the tags seperated by commas, but i am not sure how i can create a query that searches for matching tags, i dont want to have to query every article everytime someone clicks a tag. can anyone help?

You're describing a many-to-many relationship between Articles and Tags. You'd want to use an intermediate junction table to resolve that relationship.

在此输入图像描述

Now, to find all articles that match a selected tag:

SELECT a.article_title
    FROM Tag t
        INNER JOIN Article_Tag_Xref atx
            ON t.tag_id = atx.tag_id
        INNER JOIN Article a
            ON atx.article_id = a.article_id
    WHERE t.tag_name = 'SelectedTagName'

tbl_articles has articleID, etc.

tbl_tags has tagID, etc.

tbl_taggedarticles has taggedArticleID, articleID, tagID

SELECT articleID FROM tbl_taggedarticles WHERE tagID = "searchedID"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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