From 01494a7fb63fd99544ca39158dd1b48c7c6cd9b7 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Sat, 5 Dec 2015 19:17:35 +0000 Subject: Add pagination --- search.php | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/search.php b/search.php index f2acce8..bcfb2e9 100644 --- a/search.php +++ b/search.php @@ -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 `#`"); + } + + 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(); } -- cgit v1.2.3