diff options
author | Fbenas <philbeansburton@gmail.com> | 2015-12-05 19:17:35 +0000 |
---|---|---|
committer | Fbenas <philbeansburton@gmail.com> | 2015-12-05 19:17:35 +0000 |
commit | 01494a7fb63fd99544ca39158dd1b48c7c6cd9b7 (patch) | |
tree | ac00105eba948db9c90c8fdb02a3dfa96f2efedf | |
parent | 5bee1bb52dff7078a1d44c6c839a19e37c4ff248 (diff) |
Add pagination
-rw-r--r-- | search.php | 53 |
1 files changed, 51 insertions, 2 deletions
@@ -24,13 +24,43 @@ try { } $args = explode(" ", $search); + $page = 1; + // Check to see if user specified pagination + $pages = array_filter($args, function($elem) { + if (strpos($elem, "#") === 0) { + return true; + } + return false; + }); + // reset the indexes after the array filter + $pages = array_merge($pages); + + if (count($pages) > 1) { + throw new Exception("Too many page choices. Enter only one `#<page>`"); + } + + if (!empty($pages)) { + // Set the page + $page = substr($pages[0], 1); + } + if (!is_int((int)$page) || $page <= 0) { + throw new Exception("Page `" . $page . "` is not valid"); + } + + + // remove the pagination from the search args + $pos = array_search("#" . $page, $args); + if ($pos) { + $args[$pos] = null; + } + // Really slow search $results = []; foreach ($output as $upload) { $match = false; foreach ($args as $arg) { - if (strpos($upload->filename, $arg) === false) { + if (!is_null($arg) && strpos($upload->filename, $arg) === false) { $match = false; break; } @@ -42,14 +72,33 @@ try { } // Return results + $out = ""; if (empty($results)) { echo "No results found\n"; } else { + $count = count($results); + $i = 0; + + $start = ($page-1)*3; + $end = $start+2; + if ($start > ($count-1)) { + throw new Exception("No results for this page"); + } foreach($results as $result) { - echo $result . "\n"; + if ($i < $start || $i > $end) { + $i++; + continue; + } + $out .= $result . "\n"; + $i++; + } + if ($count > 3) { + $out .= " - results (" . $count . ")\n"; } } + echo $out; + } catch (Exception $e) { echo $e->getMessage() . "\n";die(); } |