Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. <?php
  2. $bedRange = "";
  3. $propertyType = "";
  4. $priceRange = "";
  5.  
  6. $sourceLong = $_GET['srcLong'];
  7. $sourceLat = $_GET['srcLat'];
  8. $searchRange = $_GET['searchRadius'];
  9.  
  10. if (isset($_GET['beds_min'])) {
  11. $bedRange = " and bedrooms between " . $_GET['beds_min'] . " and " . $_GET['beds_max'];
  12. }
  13.  
  14. if (isset($_GET['property_type_id'])) {
  15. $propertyType = " and property_type_id = " . $_GET['property_type_id'];
  16. }
  17.  
  18. if (isset($_GET['price_min'])) {
  19. $priceRange = " and price >= " . $_GET['price_min'] . " and price < " . $_GET['price_max'];
  20. }
  21.  
  22. $searchLimit = "";
  23. $responseMessage = "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><response>";
  24.  
  25. $db = pg_connect("host=db1 port=5432 dbname=mls user=postgres password=choap5ou");
  26.  
  27. if (!$db) {
  28. echo "Unable to connect to database. Please try again later.";
  29. exit;
  30. }
  31.  
  32. /* Too many results will cause some serious performance hits. If the user searches a radius of 10 miles, limit to 400 results or so. */
  33. $queryString = "SELECT property_id,latitude,longitude FROM property WHERE
  34. (power((69.1*(longitude - " . $sourceLong . ")*cos(" . $sourceLat . "/57.3)),2)+POW((69.1*(latitude-" .
  35. $sourceLat . ")),2))< (" . $searchRange . "*" . $searchRange . ")" . $bedRange . $propertyType . $priceRange . $searchLimit;
  36.  
  37. $cacheString = "FROM property a WHERE (power((69.1*(longitude - " . $sourceLong . ")*cos(" . $sourceLat . "/57.3)),2)+POW((69.1*(latitude-" . $sourceLat . ")),2))< (" . $searchRange . "*" . $searchRange . ")" . $bedRange . $propertyType . $priceRange;
  38.  
  39. $result = pg_query($db,$queryString);
  40.  
  41. $property_count = pg_num_rows($result);
  42. /* Get a result ID and put it in the database */
  43. $fullResults = pg_query($db,"select cache_result_id('" . $cacheString . "')");
  44. $fullRow = pg_fetch_row($fullResults);
  45. $result_id = $fullRow[0];
  46.  
  47. /* update the result ID with the sql so we can save this query */
  48. $cacheresult = pg_query($db,"update result set query = 'a.property_id " . $cacheString . "' where result_id = " . $result_id);
  49.  
  50. $mapCount = 1;
  51. while (($row = pg_fetch_array($result)) && ($mapCount <= 200)) {
  52. $responseMessage .= '<property id="' . $row['property_id'] . '" result_id="' . $result_id . '" count="' . $property_count . '">';
  53. $responseMessage .= '<latitude>' . $row['latitude'] . '</latitude>';
  54. $responseMessage .= '<longitude>' . $row['longitude'] . '</longitude>';
  55. $responseMessage .= '</property>';
  56. $mapCount++;
  57. }
  58.  
  59. $responseMessage .= '</response>';
  60.  
  61. /* Build entry in result_cache */
  62.  
  63. echo $responseMessage;
  64. pg_close($db);
  65. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement