<?php
+$N=30;
+
/* parse ini -file */
$iniarray=parse_ini_file("config.ini");
$DBFILE=$iniarray["fspotdb"];
+$usePDO=$iniarray["usePDO"];
+$N=$iniarray["pics_per_page"];
/* end parse ini-file */
-if (isset($_REQUEST["O"]))
- $O = "".$_REQUEST["O"].",";
+if($usePDO)
+ $DB = new PDO("sqlite:$DBFILE");
else
- $O="";
+ $DB = new SQlite3($DBFILE);
+
-if (isset($_REQUEST["N"]))
- $N = "".$_REQUEST["N"];
+/* do database query */
+if (isset($_REQUEST["S"]))
+ {
+ /* 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
- $N= 25;
+ {
+ if (isset($_REQUEST["P"]))
+ $OFFSET = "".($_REQUEST["P"]*$N-$N);
+ else
+ $OFFSET = "0";
-$DB = new SQlite3($DBFILE);
+ if (isset($_REQUEST["T"]))
+ {
+ /* single tag or part of tag */
+ $tags = $_REQUEST["T"];
+ $tags = explode("+",$tags);
+ $tags = "'".implode("','",$tags)."'";
-$result = $DB->query("SELECT * FROM photos LIMIT $O $N");
+ /* 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");
-sqlite_close(DB);
+ $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)");
-$row = array();
+ }
+ else
+ {
+ $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
+ $count = $DB->query("SELECT count(*) as total FROM photos");
+ }
+ }
-$i = 0;
+/* encode result as an array */
+$tmp=array();
+if(!$usePDO)
+ {
+ /* convert results into array */
+ while($res = $result->fetchArray(SQLITE3_ASSOC))
+ $tmp[]=$res;
+ }
+else
+ {
+ foreach($result as $res)
+ $tmp[]=$res;
+ }
+$result=$tmp;
-while($res = $result->fetchArray(SQLITE3_ASSOC)){
-
- $row[$i] = $res;
-
- $i++;
-
- }
+/* 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)
+ $DB=null;
+else
+ $DB->close();
-echo json_encode($row);
?>