UNIX_TIMESTAMP()) DESC, Visited DESC"; //count how many games without offset/limit $gamescount = $pdo->prepare($query); $gamescount->bindParam(':u', $keywordq, PDO::PARAM_STR); $gamescount->execute(); $gamescount = $gamescount->rowCount(); //data for pages $total = $gamescount; $pages = ceil($total / $limit); $offset = ($page - 1) * $limit; // Prepare the paged query (if keyword isnt empty, it will be used) $games = $pdo->prepare($query . ' LIMIT :limit OFFSET :offset'); $games->bindParam(':u', $keywordq, PDO::PARAM_STR); $games->bindParam(':limit', $limit, PDO::PARAM_INT); $games->bindParam(':offset', $offset, PDO::PARAM_INT); $games->execute(); //final check to see if page is invalid if ($pages > 0) { if ($page > $pages) { http_response_code(400); } } //construct the json array $jsonData = array( "pageCount" => $pages, "pageResults" => (int)$games->rowCount(), "keyword" => $keyword ); foreach($games as $game) { $gameID = $game['id']; //id of the game $name = cleanOutput($game['Name']); //name of the game $desc = cleanOutput($game['Description']); //description of the game $visits = $game['Visited']; //visit count of the game $creation = date("m/d/Y", $game['Created']); //creation date of the game NOTE: to get the date, use UNIX_TIMESTAMP() $creatorN = getUsername($game['CreatorId']); //creator of the game username $playercount = gamePlayerCount($gameID); //players in the game $placerender = handleGameThumb($gameID); $placeInfo = array( "id" => $gameID, "name" => $name, "description" => $desc, "visits" => $visits, "creation" => $creation, "creatorName" => $creatorN, "playerCount" => $playercount, "thumbnail" => $placerender ); array_push($jsonData, $placeInfo); } // ... die(json_encode($jsonData));