$searchQuery) { switch ($column) { case 'city': $whereClauses[] = 'c.City LIKE \'%' . mysql_real_escape_string($searchQuery, $conn) . '%\''; break; case 'country': $whereClauses[] = 'co.Country LIKE \'%' . mysql_real_escape_string($searchQuery, $conn) . '%\''; break; } } } if (!empty($whereClauses)) { $query .= 'WHERE ' . implode("\n\t\tAND ", $whereClauses) . "\n\t\t"; } if (!empty($sortColumn)) { switch ($sortColumn) { case 'region': $query .= 'ORDER BY r.Region'; break; case 'country': $query .= 'ORDER BY co.Country'; break; case 'city': case 'latitude': case 'longitude': $query .= 'ORDER BY c.' . ucfirst($sortColumn); break; default: $fail = true; break; } if (!isset($fail) && $sortDirection === 'desc') { $query .= ' DESC'; } } $query .= ' LIMIT ' . $limit . ' OFFSET ' . $offset; $result = mysql_query($query, $conn); if (!$result) { header('HTTP/1.1 500 Internal Server Error'); echo 'The database exploded!'; exit; } $json = new stdClass(); $json->records = array(); $json->offset = (int)$offset; while ($row = mysql_fetch_assoc($result)) { $obj = new stdClass(); $obj->city = $row['City']; $obj->latitude = (float)$row['Latitude']; $obj->longitude = (float)$row['Longitude']; $obj->region = $row['Region']; $obj->country = $row['Country']; $obj->countryPopulation = (int)$row['Population']; $json->records[] = $obj; } $json->totalRecordCount = (int)mysql_result(mysql_query('SELECT FOUND_ROWS()', $conn), 0); header('Content-Type: application/json'); echo json_encode($json); exit; ?>