X-Git-Url: https://git.nubati.net/cgi-bin/gitweb.cgi?p=photo-tags.git;a=blobdiff_plain;f=getjson.php;h=cc93c7c352b95f0b6c42466c7e3cadf02a929155;hp=6026776775ea5a5278becd77e0289b806b0bfceb;hb=fe9797bd60e0027fac4bb989619eec1e428aabd0;hpb=db1b03220d1d9eced86e6177f6787e59e5fd69a5;ds=sidebyside diff --git a/getjson.php b/getjson.php index 6026776..cc93c7c 100644 --- a/getjson.php +++ b/getjson.php @@ -9,46 +9,94 @@ $usePDO=$iniarray["usePDO"]; $N=$iniarray["pics_per_page"]; /* end parse ini-file */ -if (isset($_REQUEST["P"])) - $OFFSET = "".($_REQUEST["P"]*$N-$N).","; -else - $OFFSET = ""; - if($usePDO) $DB = new PDO("sqlite:$DBFILE"); else $DB = new SQlite3($DBFILE); -$result = $DB->query("SELECT * FROM photos LIMIT $OFFSET $N"); -$row = array(); +/* 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 + { + 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)"); -$i = 0; + } + else + { + $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N"); + $count = $DB->query("SELECT count(*) as total FROM photos"); + } + } +/* encode result as an array */ +$tmp=array(); if(!$usePDO) { /* convert results into array */ - $tmp=array(); - while($res = $result->fetchArray(SQLITE3_ASSOC)){ + while($res = $result->fetchArray(SQLITE3_ASSOC)) + $tmp[]=$res; + } +else + { + foreach($result as $res) $tmp[]=$res; - } - $result=$tmp; } +$result=$tmp; -foreach ($result as $res) -{ - $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($row); +echo json_encode($return); /* close the database */ if($usePDO) $DB=null; else - sqlite_close($DB); + $DB->close(); + ?>