From bcc4cfa62ccc46113e1e47b305156caaa853723c Mon Sep 17 00:00:00 2001 From: Hannes Magnusson Date: Wed, 4 Dec 2013 00:03:32 -0800 Subject: [PATCH] Fix counting logic.. as we skip an item if it doesn't exist Also, don't allow more then 50 pics --- images/elephpants.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/images/elephpants.php b/images/elephpants.php index 3375f9d10..3cee268ad 100644 --- a/images/elephpants.php +++ b/images/elephpants.php @@ -20,7 +20,7 @@ // determine how many images to serve. if (isset($_REQUEST['count'])) { - $count = intval($_REQUEST['count']); + $count = min(intval($_REQUEST['count']), 50); } else { header('HTTP/1.1 400', true, 400); print json_encode(array( @@ -46,10 +46,11 @@ if (!$photos || !is_array($photos)) { // prepare requested number of elephpants at random. shuffle($photos); $elephpants = array(); -foreach ($photos as $n => $photo) { +$got = 0; +foreach ($photos as $photo) { // stop when we have the requested number of photos. - if ($n == $count || $n > 20) { + if ($got == $count) { break; } @@ -58,6 +59,7 @@ foreach ($photos as $n => $photo) { continue; } + $got++; // add photo to response array. $elephpants[] = array( 'title' => $photo['title'], @@ -67,3 +69,4 @@ foreach ($photos as $n => $photo) { } print json_encode($elephpants); +