简体   繁体   中英

get all related tags?

this is very tricky. i´ve got a table with 2 columns thread_tag_map: thread_id and tag_name.

  thread_id tag_name
  1         football
  1         manchester
  2         manchester
  2         england
  3         england
  3         queen
  4         queen
  4         diana

as you can see one thread can have multiple tags, and this give us a linking effect of tags.

if you type the tag football i want it to show all related tags to football. that is to say manchester, england, queen and diana.

so here is what ive coded so far:

    // get all thread_id:s for tag_name
    $query = "SELECT *
            FROM thread_tag_map
            WHERE tag_name = 'football'";

    $result1 = mysqli_query($conn, $query) or die ("Couldn't execute query: " . mysqli_error($conn));

    // get all tag_name:s for each thread_id
    while($row = mysqli_fetch_assoc($result1))
    {
        $thread_id = $row['thread_id'];

        $query = "SELECT *
                FROM thread_tag_map
                WHERE thread_id = $thread_id";

        $result2 = mysqli_query($conn, $query) or die ("Couldn't execute query: " . mysqli_error($conn));

    // add each tag to array
    while($row = mysqli_fetch_assoc($result2))
    {
        $tag_array[] = $row['tag_name'];
      }
   }

but this just give me football and manchester. i dont know how i can proceed to make it a good code to loop (for loop?) it through. maybe there is 100 related tags.

i think you understand the idea. has someone done this before?

You can formalize what you're trying to do a bit here with graph theory. You want all the connected nodes to a given one, given an adjacency list (sort-of). To do this, you would want to do a breadth first search of the graph. This is important to avoid cycles.

The representation you've chosen isn't entirely efficient, though, but could certainly work.

In pseudo-code, your algorithm should look something like this:

interesting-tags = input-tag
output = empty
for tag in interesting-tags:  (Must be in order)
    select related-tags to tag
    for newtag in related-tags:
        if newtag is not in output:
             append newtag to interesting-tags and output

return output

So here, interesting-tags should be some form of a Queue, since you need to add new items to the back and take them from the front.

Output should be a Set data type, since you need to check if things are already in the set, and add them.

I am, however, unfamiliar with PHP and so I don't know what is available to you. At very least, you can implement the operations you need with just an array, even if it might not be totally efficient, it'll work for a few tags.

If you're building some kind of forum / discussion board, and if by related tags you mean all the tags that were posted in threads that relate with a board / forum, would't it be easier to just select them all? Since you create tags based on the thread, they should be all correctly placed, so you just have to get them all.

Also, I'd suggest you create a temporary table just for that purpose, where you store the "related tags" once they're needed in the first place, saving the array in that table for future search, and updating it when some new word is given as a tag.

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