UNIX_TIMESTAMP()) DESC, lastseen DESC"; //count how many users without offset/limit $usercount = $pdo->prepare($query); $usercount->bindParam(':u', $keywordq, PDO::PARAM_STR); $usercount->execute(); $usercount = $usercount->rowCount(); //data for pages $total = $usercount; $pages = ceil($total / $limit); $offset = ($page - 1) * $limit; // Prepare the paged query (if keyword isnt empty, it will be used) $users = $pdo->prepare($query . ' LIMIT :limit OFFSET :offset'); $users->bindParam(':u', $keywordq, PDO::PARAM_STR); $users->bindParam(':limit', $limit, PDO::PARAM_INT); $users->bindParam(':offset', $offset, PDO::PARAM_INT); $users->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)$users->rowCount(), "keyword" => $keyword ); foreach($users as $user) { $id = $user['id']; $username = $user['username']; $blurb = cleanOutput($user['blurb'], false); //pass false to not add html linebreaks $sitestatus = siteStatus($id); $lastseen = date("m/d/Y", $user['lastseen']); $thumbnail = getPlayerRender($user['id']); $jsonInfo = array( "id" => $id, "username" => $username, "blurb" => $blurb, "siteStatus" => $sitestatus, "lastseen" => $lastseen, "thumbnail" => "https://api.alphaland.cc/users/thumbnail?userId=".$id."&headshot=true" ); array_push($jsonData, $jsonInfo); } // ... die(json_encode($jsonData));