Weblog entry #171 for Steve
There first piece of code from my online dating site has now been added to this site - something nice and simple which was easy to migrate.
When you perform a tag search you can now view a list of related tags. This is actually pretty useful, since it may be used narrow down the search results.
Over the next week or two, when I have more time, I'll import some other changes.
On a separate note I promised to rename a user's account a last night. It turned out to be very difficult.
In the future I'm going to be money-grabbing and say if you want your account renamed it will cost you. That should ensure people never ask again ;)
Comments on this Entry
--
"It's Not Magic, It's Work"
Adam
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
Thanks!
As for the how it is done, that requires that you first know my table structure...
The tags table is pretty simple and looks like this:
# # create new tags table # CREATE TABLE `tags` ( \ `id` int(11) NOT NULL auto_increment, \ `user_id` int(11) NOT NULL default '0', \ `tag` varchar(25) NOT NULL default '', \ root` int(11) NOT NULL default '0', \ `type` char(1) default NULL, \ PRIMARY KEY (`id`, `type`, `root` ), \ KEY `id` (`user_id`), \ KEY `tag` (`tag`), \ KEY `root` (`root`) \ );
Here the fields are as follows:
- id - The ID of this tag entry. (Not really used.)
- user_id - The ID of the user who added this tag.
- tag - The text of the tag itself.
- root - The root node upon which this tag has been applied
- type - The type of tag.
As you know tags can be applied to articles, polls, and weblog entries. Each of these things is a different type. So for example:
- A tag on article 1: root=1, type='a'.
- A tag on poll 1: root=1, type='p'.
- A tag on a weblog entry: root=x, type='w'.
(In the dating site there are more tagging types; "i" for image, "p" for person, "l" for location, "k" for kink ..)
So to find a related tag what we want to do is find tags with the same root and type as one with the given text. This is done with a self-join, and the actual query looks like this:
SELECT DISTINCT(a.tag) FROM tags AS a \
JOIN tags b \
ON b.root=a.root \
AND a.type=b.type WHERE b.tag=?
This gives a nice list of all tags applied to the same object - and which are therefore related.
In terms of code the complete change can be found in this changeset.
[ Parent | Reply to this comment ]