diff options
author | Arun Persaud <arun@nubati.net> | 2011-11-29 22:17:38 -0800 |
---|---|---|
committer | Arun Persaud <apersaud@lbl.gov> | 2011-11-29 22:17:38 -0800 |
commit | 540c8909ac127c623eb2e024c433524898e2ed75 (patch) | |
tree | bdbbb947cfd7dc07401186c3336ed738ea35fbe0 | |
parent | 710c7defb7a4a4de3830c4beff12c7da0c8c3391 (diff) | |
download | photo-tags-540c8909ac127c623eb2e024c433524898e2ed75.tar.gz photo-tags-540c8909ac127c623eb2e024c433524898e2ed75.tar.bz2 photo-tags-540c8909ac127c623eb2e024c433524898e2ed75.zip |
changed tag search to use AND for multiple tags
-rw-r--r-- | getjson.php | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/getjson.php b/getjson.php index 97f8c49..d9b0891 100644 --- a/getjson.php +++ b/getjson.php @@ -36,21 +36,30 @@ else /* single tag or part of tag */ $tags = $_REQUEST["T"]; $tags = explode(",",$tags); + $nrtags = count($tags); foreach ($tags as $key => $value) $tags[$key]=sqlite_escape_string(trim($value)); $tags = "'".implode("','",$tags)."'"; /* individual tags are seperated by ',' */ - $result = $DB->query("SELECT base_uri, filename FROM photos ". - " left join photo_tags on photos.id=photo_tags.photo_id ". - " left join tags on tags.id=photo_tags.tag_id ". - " where tags.name COLLATE NOCASE in ($tags) LIMIT $OFFSET, $N"); - - $count = $DB->query("SELECT count(*) as total FROM photos ". - " left join photo_tags on photos.id=photo_tags.photo_id ". - " left join tags on tags.id=photo_tags.tag_id ". - " where tags.name COLLATE NOCASE in ($tags)"); + /* use and AND query between tags as a default + a good explanation on different ways of doing this can be found at: + http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html + */ + $result = $DB->query("SELECT base_uri, filename FROM photo_tags pt, photos p, tags t". + " WHERE pt.tag_id = t.id". + " AND (t.name COLLATE NOCASE IN ($tags))". + " AND p.id = pt.photo_id ". + " GROUP BY p.id HAVING COUNT( p.id )=$nrtags". + " LIMIT $OFFSET, $N"); + + $count = $DB->query("SELECT count(*) as total FROM photo_tags pt, photos p, tags t". + " WHERE pt.tag_id = t.id". + " AND (t.name COLLATE NOCASE IN ($tags))". + " AND p.id = pt.photo_id ". + " GROUP BY p.id HAVING COUNT( p.id )=$nrtags". + " LIMIT $OFFSET, $N"); } else { |