diff --git a/mdcountdata.php b/mdcountdata.php index 55c406a..ae93166 100644 --- a/mdcountdata.php +++ b/mdcountdata.php @@ -1,5 +1,9 @@ tz; } -function countsort($sort, $out) { +function countsort($sort, $out, $limit = null) { $data = json_decode($out); // ascending if($sort === 'a' || $sort === '') { @@ -63,10 +91,13 @@ function countsort($sort, $out) { }); } // done! - return json_encode($data); + if($limit !== null && ($limit > 0 && $limit < count($data))){ + $data = array_slice($data, 0, $limit); + } + return json_encode($data); } -function timesort($sort, $out) { +function timesort($sort, $out, $limit = null) { $data = json_decode($out); // ascending if($sort === 'a' || $sort === '') { @@ -83,10 +114,13 @@ function timesort($sort, $out) { }); } // done! - return json_encode($data); + if($limit !== null && ($limit > 0 && $limit < count($data))){ + $data = array_slice($data, 0, $limit); + } + return json_encode($data); } -function idsort($sort, $out) { +function idsort($sort, $out, $limit = null) { $data = json_decode($out); // ascending if($sort === 'a' || $sort === '') { @@ -103,20 +137,31 @@ function idsort($sort, $out) { }); } // done! - return json_encode($data); + if($limit !== null && ($limit > 0 && $limit < count($data))){ + $data = array_slice($data, 0, $limit); + } + return json_encode($data); } // MUST be done like this for PHP files that are 'linked' $queries = array(); - parse_str($_SERVER['QUERY_STRING'], $queries); +// return a single counter by ID $id = (isset($queries['id']) ? strtolower($queries['id']) : null); +// return all counters, ordered by count $csort = (isset($queries['csort']) ? strtolower($queries['csort']) : null); +// return all counters, ordered by time of last count $tsort = (isset($queries['tsort']) ? strtolower($queries['tsort']) : null); +// return all counters, ordered by ID $isort = (isset($queries['isort']) ? strtolower($queries['isort']) : null); +// for sorts, limit number of counters returned +$limit = (isset($queries['limit']) ? $queries['limit'] : null); //$id = null; //$csort = 'd'; +//$tsort = null; +//$isort = null; +//$limit = 1; $_idlist = json_decode(file_get_contents(VALID_COUNTERS)); $idlist = array_map('strtolower', $_idlist->valid); @@ -171,17 +216,17 @@ function idsort($sort, $out) { // is sorting selected? if($csort !== null) { // by count - $out = countsort($csort, $out); + $out = countsort($csort, $out, $limit); } if($tsort !== null) { // by time - $out = timesort($tsort, $out); + $out = timesort($tsort, $out, $limit); } if($isort !== null) { // by ID - $out = idsort($isort, $out); + $out = idsort($isort, $out, $limit); } $result = $out;