Advertisement
Guest User

Code Sample

a guest
Nov 26th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. <?php
  2. /**
  3. * @description This file will be responsible for match user location and Dealer location based on there longitude and latitude
  4. * @author Luxury Buys Today
  5. *
  6. */
  7. /**
  8. * @description this function will return you nearest dealers id
  9. * @param array $coordinate
  10. * @param int $limit
  11. * @param int $max_distance
  12. * @param int | null $cat
  13. * @return Object
  14. * @to-do Documentation to be improved
  15. * @to-do Code need to be much cleaner
  16. */
  17. function get_matched_dealer($coordinate=array(),$cat=null,$limit= 20,$max_distance=50){
  18. // var_dump($cat);
  19. global $wpdb;
  20. $return_array=new stdClass(); //
  21. $user_latitude = $coordinate['latitude'] ? $coordinate['latitude'] : '0';
  22. $user_longitude = $coordinate['longitude'] ? $coordinate['longitude'] : '0';
  23. /**
  24. * Having some problem to query from certain category, need to fix it but meanwhile going to filter by cat after retrieve from database, and let's multiply it by 8 to
  25. * @to-do filter part have to be rewritten
  26. */
  27. // $user_latitude = 36.0979230;
  28. // $user_longitude = -80.1036380;
  29. $dealers=$wpdb->get_results(
  30. "SELECT DISTINCT post_id as dealer_id,address, ( 3959 * acos( cos( radians($user_latitude) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($user_longitude) ) + sin( radians($user_latitude) ) * sin( radians( lat ) ) ) ) AS distance FROM
  31. (
  32. SELECT a.post_id, a.meta_value as lat, b.meta_value as lng, c.meta_value as address
  33. FROM
  34. lbt_postmeta as a,
  35. lbt_postmeta as b,
  36. lbt_postmeta as c,
  37. lbt_term_relationships as d
  38. WHERE a.meta_key = 'geo_latitude'
  39. AND b.meta_key= 'geo_longitude'
  40. AND c.meta_key= 'geo_address'
  41. AND a.post_id = b.post_id
  42. and a.post_id = c.post_id
  43. AND a.post_id = d.object_id
  44. AND d.term_taxonomy_id = $cat
  45. )
  46. as f_table HAVING distance < $max_distance ORDER BY distance LIMIT 0, $limit"
  47. ); // end of get_result method
  48. if($dealers) {
  49. $return_array->dealers=array();
  50. $return_array->dealer_ids=array();
  51. foreach($dealers as $dealer){
  52. $return_array->dealer_ids[]=$dealer->dealer_id;
  53. $return_array->dealers[]=$dealer;
  54. }
  55. //$return_array->dealers_id=$dealers_id;
  56. }
  57. else {
  58. // echo "no matching dealers";
  59. return;
  60. }
  61. return $return_array;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement