summaryrefslogtreecommitdiffstats
path: root/getjson.php
diff options
context:
space:
mode:
authorArun Persaud <arun@nubati.net>2011-11-25 22:27:33 -0800
committerArun Persaud <apersaud@lbl.gov>2011-11-25 22:27:33 -0800
commitfe9797bd60e0027fac4bb989619eec1e428aabd0 (patch)
treee6770378d3efa01e5d0518be35899147b746a6aa /getjson.php
parent4849d7271c75cace9faffc1353c22101b84c49bb (diff)
downloadphoto-tags-fe9797bd60e0027fac4bb989619eec1e428aabd0.tar.gz
photo-tags-fe9797bd60e0027fac4bb989619eec1e428aabd0.tar.bz2
photo-tags-fe9797bd60e0027fac4bb989619eec1e428aabd0.zip
initial support for tag search
* only a single tag works * need to match any case (at the moment the tag has to be capitilized correctly)
Diffstat (limited to 'getjson.php')
-rw-r--r--getjson.php71
1 files changed, 56 insertions, 15 deletions
diff --git a/getjson.php b/getjson.php
index 0b414be..cc93c7c 100644
--- a/getjson.php
+++ b/getjson.php
@@ -16,18 +16,46 @@ else
/* do database query */
-if (isset($_REQUEST["P"]))
+if (isset($_REQUEST["S"]))
{
- $OFFSET = "".($_REQUEST["P"]*$N-$N);
-
- $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
+ /* single tag or part of tag */
+ $tag = $_REQUEST["S"];
+ /* individual tags are separated by '+' */
+ $result = $DB->query("SELECT name FROM tags where name like \"%$tag%\"");
+ $count = $DB->query("SELECT 1");
}
-else if (isset($_REQUEST["T"]))
+else
{
- $result = $DB->query("SELECT count(*) as total FROM photos");
+ if (isset($_REQUEST["P"]))
+ $OFFSET = "".($_REQUEST["P"]*$N-$N);
+ else
+ $OFFSET = "0";
+
+ if (isset($_REQUEST["T"]))
+ {
+ /* single tag or part of tag */
+ $tags = $_REQUEST["T"];
+ $tags = explode("+",$tags);
+ $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 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 in ($tags)");
+
+ }
+ else
+ {
+ $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
+ $count = $DB->query("SELECT count(*) as total FROM photos");
+ }
}
-else
- $result=null;
/* encode result as an array */
$tmp=array();
@@ -35,20 +63,33 @@ if(!$usePDO)
{
/* convert results into array */
while($res = $result->fetchArray(SQLITE3_ASSOC))
- {
- $tmp[]=$res;
- }
+ $tmp[]=$res;
}
else
{
foreach($result as $res)
- {
- $tmp[]=$res;
- }
+ $tmp[]=$res;
}
$result=$tmp;
-echo json_encode($result);
+/* encode count as an array */
+$tmp=array();
+if(!$usePDO)
+ {
+ /* convert results into array */
+ while($res = $count->fetchArray(SQLITE3_ASSOC))
+ $tmp[]=$res;
+ }
+else
+ {
+ foreach($count as $res)
+ $tmp[]=$res;
+ }
+$count=$tmp;
+
+$return=array($count,$result);
+
+echo json_encode($return);
/* close the database */
if($usePDO)