/    Sign up×
Community /Pin to ProfileBookmark

Ok, so i’m wanting to implement a tagging system into something that i am working on. So far, i have just been planning all the functionality that i want to have. As in, i have done no coding for it yet.

Everything is pretty simple, except one thing. When doing a search, i want a list of tags that are common to every result returned by the search.

This is the layout i had planned so far.

[code]CREATE TABLE `object` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`value` VARCHAR(512) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `tag` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(64) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `tagMap` (
`tagId` INT(10) UNSIGNED NOT NULL ,
`objectId` INT(10) UNSIGNED NOT NULL ,
UNIQUE (`tagId`,`objectId`)
);[/code]

to post a comment

1 Comments(s)

Copy linkTweet thisAlerts:
@toenailsinauthorDec 05.2011 — i guess i will have to use PHP to determine what tags are common to all results.

SELECT
<span><code>object</code></span>.<span><code>id</code></span>,
<span><code>object</code></span>.<span><code>value</code></span>,
GROUP_CONCAT(<span><code>tag</code></span>.<span><code>id</code></span>) AS <span><code>tags</code></span>
FROM <span><code>object</code></span>
LEFT JOIN <span><code>tagMap</code></span> ON ( <span><code>tagMap</code></span>.<span><code>objectId</code></span> = <span><code>object</code></span>.<span><code>id</code></span> )
LEFT JOIN <span><code>tag</code></span> ON ( <span><code>tagMap</code></span>.<span><code>tagId</code></span> = <span><code>tag</code></span>.<span><code>id</code></span> )
WHERE <span><code>object</code></span>.<span><code>value</code></span> LIKE "&amp;#37;?%"
GROUP BY <span><code>object</code></span>.<span><code>id</code></span>
LIMIT 30
OFFSET ?


[code=php]// search result
$results = array(...);

// explode tag ids
$l = $i = count($results);
while ($i--){
$results[$i] = explode(',', $results[$i]);
}

// determine common tags
$commonTags = array();
$i = $l;
while ($i--){
$j = count($results[$i]->tags);
tag: while ($j--){
// check if already in list
if (in_array($results[$i]->tags[$j], $commonTags)){
continue;
}
// search all other results
$k = $l;
while ($k--){
if (!in_array($results[$i]->tags[$j], $results[$k])){
// jump to the next tag
continue tag;
}
}
// add to the stack
$commonTags[] = $results[$i]->tags[$j];
}
}[/code]
×

Success!

Help @toenailsin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...