diff options
-rw-r--r-- | getjson.php | 71 | ||||
-rw-r--r-- | index.php | 53 |
2 files changed, 101 insertions, 23 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) @@ -14,6 +14,11 @@ if(isset($_REQUEST["page"])) else $page = 1; +if(isset($_REQUEST["tag"])) + $tags = $_REQUEST["tag"]; +else + $tags = ""; + ?> <html> <title><?php echo htmlspecialchars($title) ?></title> @@ -30,6 +35,15 @@ else <button class="next" onclick="right()">next </button> <div class="permalink"></div> + +<div class="tagsearch"> +<form method="get" action=""> + Tags: <input list="MyTags" id="MyTagsInput" type="text" value="" /> + <datalist id="MyTags"> + </datalist> +</form> +</div> + <div class="index"></div> <div class="pics"> </div> @@ -41,27 +55,51 @@ else <script type="text/javascript" > - var pics = d3.select(".pics").append("ul"); var page=<?php echo $page ?>; var N=<?php echo $N ?>; +var T="<?php echo $tags ?>"; var count=0; +/* populate data list with tags*/ +d3.json("<?php echo $webbase?>/getjson.php?S", function(json) { + d3.select("#MyTags").selectAll("option").data(json) + .enter().append("option").attr("value",function(d) {return d.name}); + }); + +/* update form to point to new link */ +d3.select("input").on("keyup", function(d) { + d3.select('form').attr("action","<?php echo $webbase?>/tag/"+document.getElementById('MyTagsInput').value); +}); + function myreload(a) { - d3.json("<?php echo $webbase?>/getjson.php?T=1", function(json) { + d3.select(".debug").text("T,P,N ="+T+" "+a+" "+N); + + if(T!="") + url = "<?php echo $webbase?>/getjson.php?T="+T+"&P="+a; + else + url = "<?php echo $webbase?>/getjson.php?P="+a; + + d3.json(url, function(json) { + + /* update index */ s="page "; - n = json[0].total/N; + n = json[0][0].total/N; for(i=1;i<=n+1;i++) { - s+=" <a href=\"<?php echo $webbase?>/page/"+i+"\">"+i+"</a>"; + s+=" <a href=\"<?php echo $webbase?>"; + if(T!="") + s+="/tag/"+T; + s+="/page/"+i+"\">"+i+"</a>"; } d3.select(".index").html(s); - }); - d3.json("<?php echo $webbase?>/getjson.php?P="+a, function(json) { + + /* update pics */ count=0; pics.selectAll("li").remove(); - pics.selectAll("li").data(json) + picdata=json[1]; + pics.selectAll("li").data(picdata) .enter().append("li") .append("a") .attr("href",function(d) { @@ -81,7 +119,6 @@ function myreload(a) { permalink="<?php echo $webbase ?>/page/"+page; d3.select(".permalink").html("Permalink: <a href=\""+permalink+"\">"+permalink+"</a>"); - d3.select(".debug").text("P,N ="+a+" "+N); } function left() { |