Check my other question with bounty:
Finding similar number patterns in table
I’m trying to implement an Interesting Tags feature. For reference, this is how it works on SO:
- I add into the “interesting” list my interested tags (like php, mysql, jquery and so on).
- Then, if any of the displayed questions has some of the tags in my list, it makes the background orange.
I understand how to use jQuery to do that (there are related questions on that), but can’t figure out how to implement the back-end portion using MySQL!
So here’s my question: How is it done? I imagine it working like this:
- There is a row in mysql for every member, let’s call it “interested_tags”.
- After I write and submit my tag through input, it is being written in a row “interested_tags”.
-
Then, the main page has a query which shows all answers and it always checks question’s tags with mine tags using strpos like this:
if(strpos($question_tags, $my_tags) === true) { //and here will be made background orange }
Am I thinking right or is there any way to do it?
EDIT: So, can you show me an example or give me some tips how to implement this with many-to-many relationships? Thanks.
As mentioned in the other answers, there’s most likely a many-to-many relationship with between users and tags, represented as an own table. I made a SQL demo of a simplified case. The
InterestingTagstable is the table connecting what user is interested in what tags.Update: Added php demo.
Remember to change your mysql constants before running the demo
What this does is running two queries to the DB:
To “mark” a question with its tags, it adds a
classfor each tag it belongs to — e.g. a question tagged withjQuery(where jQuery has the ID1) andphp(with ID2) will have the classestagged-1andtagged-2.Now combining this with the other query, fetching the interesting tags, you just have to select the questions having classes corresponding to the interesting tags and style them. For example, if you’re interested in the tags with ID
1and3, it would be the following jQuery code$('.tagged-1, .tagged-3').addClass('interesting-tag');