I need help with php «tagsystem»

Hi there, I am an 17 year old highschooler and i’m creating a simple website which allow users to select «tags» that represent their personality, interest etc.

I want a user to be able to select tags from a specific topic and press submit, the tags will be stored in a table, and put in the user’s description box visible to other users

However, I don’t know where to start with the code for this - where to store and save the selected tags for users, and save them in the user’s description box. I have created a database and a table for users, and a simple login system in order to create a user. I could really use some help, thank you.

I am not really sure if i understand it right that you want to use the tags on topics For this explanation i will use tags on topics.

Create a table Topics with the columns you like but also with a primary key column:

  1. topicID (integer, primary key, autoincrement)

Create a table tags with two columns:

  1. tagID (integer, primary key, autoincrement)
  2. name (varchar(64))

To couple MANY tags to MANY topics you will need a JOIN table with two columns:

  1. topicID (integer, primary key)
  2. tagID (integer, primary key)

Now you will be able to add Topics to the topic Table, to add Tags to the tag table and to couple tags on topics by adding a topicID and tagID to the JOIN table which you best can call topic_tag.

Now when you show a topic and you would like to get all tags related to this topic you could write a SELECT query with a JOIN:

SELECT tag.tagID, tag.name FROM tag
JOIN topic_tag ON tag.tagID = topic_tag.tagID
WHERE topic_tag.topicID = ?

Or get the topic and all related tags in one time:

SELECT topic.topicID, topic.title, tag.tagID, tag.name FROM topic
LEFT JOIN topic_tag ON topic.topicID = topic_tag.topicID
LEFT JOIN tag ON tag.tagID = topic_tag.tagID
WHERE topic.topicID = ?

I used LEFT JOINs so that even if a topic do not have a single tag you still get the topic data. tag.id and tag.name will be NULL in that case.

1 Like

Thank you so much for your response! I think I got the solution that I needed :slight_smile:

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service