Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7686379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:28:01+00:00 2026-05-31T19:28:01+00:00

I am building a ‘tag’ system. I quoted it because its not quite a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:28:02+00:00Added an answer on May 31, 2026 at 7:28 pm

    The database structure is good with those 3 tables. But there’s a little problem with your query. Not a syntax one thou. Let’s take an example. users search for content with tags ‘a’ and ‘b’. If you have a content with only tag ‘a’ it will turn up as a result. Also contents with only ‘b’ tags. You should append to query something like:

    HAVING COUNT(t.tag_id) = x

    , where x is the length of the numbers of tags you are querying with (in my example 2, in yours 3). This way a content will be considered result if all items of the given set was matched. This only works if there are no duplicate entries in any of these tables.

    Also, instead of the loop use implode.

    $string = implode(",", $tagarray);
    

    This way you don’t need to bother removing the last comma.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Building an inventory system. I have lots of products and each product has three
Building a Django app on a VPS. I am not very experienced with setting
building libraries with waf is nice and I like the lib<targetname> naming scheme. But
Building a site on CargoCollective, but it appears they use jQuery 1.4.2 which seems
Building my code (below) returns error 'imread' is not a member of 'cv' .
Building a website. When I order my tags like this, LightCycle works but Lightbox
Building a GUI system and I have a few classes for different GUI components
Building a simple asp.net web app just to test things out (will obviously refactor
Building the project throws the following error from AAPT. I've rebuilt the project, but
Building a multilingual application that should work with the most prevalent global alphabets but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.