I am building a 'tag' system. I quoted it because its not quite a tag system, but close enough.
Obviously, when dealing with tags, the user may very well use multiple tags and expect to filter the results to an even greater depth.
The problem I am coming across is how exactly to do this.
there are 2 ways I can think of doing it so far, but I would be greatly appreciative if someone out there can explain the correct way.
way 1) Do an individual query for each tag then sort/combine/play with the data afterward. way 2)
SELECT i2t.item_id
FROM Item2Tag i2t
INNER JOIN Tag t
ON i2t.tag_id = t.tag_id
WHERE t.tag_text IN ('beach','cloud','waves')
GROUP BY i2t.item_id
The above is my query. This is based on a modified version of Toxi's solution. Hopefully you see whats going on...essentially there are 3 tables, 1 for info, 1 for tags, and another to connect them.
This is currently what I am doing: I substitute 'beach','cloud','waves' with a variable. Then, within the PHP code, I convert the array to a string with each word separated by comma's and simply insert it into the query. so something like :
foreach ($tagarray as $tag){
$string = .$tag1.", " }
That is of course a bit over simplified, since I would need to correctly end the last tag without a comma..but hopefully you get the point.
so, $string becomes equal to 'tag1, tag2, tag3'
SELECT i2t.item_id
FROM Item2Tag i2t
INNER JOIN Tag t
ON i2t.tag_id = t.tag_id
WHERE t.tag_text IN ('$string')
GROUP BY i2t.item_id
Is there a better way to do this?