6 $iniarray=parse_ini_file("config.ini");
7 $DBFILE=$iniarray["fspotdb"];
8 $usePDO=$iniarray["usePDO"];
9 $N=$iniarray["pics_per_page"];
10 /* end parse ini-file */
13 $DB = new PDO("sqlite:$DBFILE");
15 $DB = new SQlite3($DBFILE);
18 /* do database query */
19 if (isset($_REQUEST["S"]))
21 /* single tag or part of tag */
22 $tag = sqlite_escape_string($_REQUEST["S"]);
24 $result = $DB->query("SELECT name FROM tags");
25 $count = $DB->query("SELECT 1");
29 if (isset($_REQUEST["P"]))
30 $OFFSET = "".(intval($_REQUEST["P"])*$N-$N);
34 if (isset($_REQUEST["T"]))
36 /* single tag or part of tag */
37 $tags = $_REQUEST["T"];
38 $tags = explode(",",$tags);
39 $nrtags = count($tags);
40 foreach ($tags as $key => $value)
41 $tags[$key]=sqlite_escape_string(trim($value));
42 $tags = "'".implode("','",$tags)."'";
44 /* individual tags are seperated by ',' */
46 /* use and AND query between tags as a default
47 a good explanation on different ways of doing this can be found at:
48 http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html
50 $result = $DB->query("SELECT base_uri, filename FROM photo_tags pt, photos p, tags t".
51 " WHERE pt.tag_id = t.id".
52 " AND (t.name COLLATE NOCASE IN ($tags))".
53 " AND p.id = pt.photo_id ".
54 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags".
55 " LIMIT $OFFSET, $N");
57 $count = $DB->query("SELECT count(*) as total FROM photo_tags pt, photos p, tags t".
58 " WHERE pt.tag_id = t.id".
59 " AND (t.name COLLATE NOCASE IN ($tags))".
60 " AND p.id = pt.photo_id ".
61 " GROUP BY p.id HAVING COUNT( p.id )=$nrtags".
62 " LIMIT $OFFSET, $N");
66 $result = $DB->query("SELECT * FROM photos LIMIT $OFFSET, $N");
67 $count = $DB->query("SELECT count(*) as total FROM photos");
71 /* encode result as an array */
75 /* convert results into array */
76 while($res = $result->fetchArray(SQLITE3_ASSOC))
81 foreach($result as $res)
86 /* encode count as an array */
90 /* convert results into array */
91 while($res = $count->fetchArray(SQLITE3_ASSOC))
96 foreach($count as $res)
101 $return=array($count,$result);
103 echo json_encode($return);
105 /* close the database */