2011-09-30 3 views
0

내 프레임 워크에 Codeigniter를 사용하고 있으며 문서를 검토했지만 해결 방법을 찾지 못했습니다.각 결과의 끝 부분에 변수를 추가하십시오.

데이터를 데이터베이스에서 선택 했으므로 검색된 각 레코드 끝에 거리를 표시해야합니다.

Example of json: {"details":[{"country":"United States","_id":"3892","lat":"39.954559","lng":"-82.837608","admin_level_1":"Ohio","locale":"Columbus"}]} 

이것은 거리가 데이터베이스에 없다는 것을 명심해야합니다. 즉석에서 계산됩니다.

Example of json: {"details":[{"country":"United States","_id":"3892","lat":"39.954559","lng":"-82.837608","admin_level_1":"Ohio","locale":"Columbus", "distance": "1.2 Mi"}]} 

각 결과 끝에 추가 할 거리를 구하는 방법에 대한 아이디어가 있으십니까?

 $dbStations->select('lat'); 
     $dbStations->select('lng'); 
     $dbStations->select('_id'); 
     $stations = $dbStations->get('stDetails'); 
     foreach ($stations->result() as $station) { 
      $lat2 = $station->lat; 
      $lng2 = $station->lng; 
      $stId = $station->_id; 
      $distance = $this->distance->gpsdistance($lat1, $lng1, $lat2, $lng2, "M"); 
       if ($distance <= $rad) { 
       //at this point the code has determined that this id is within 
       //the preferred distance. 
       $stationArr[] = $stId; 
      } 
     } 
      $dbStations->select('country, _id, lat, lng, admin_level_1, locale'); 
      $dbStations->where_in('_id', $stationArr); 
      $stations = $dbStations->get('stationDetails'); 
     echo json_encode(array('stations' => $stations->result())); 
    } 
+0

'$ dbStations-> get ('stDetails')'및'$ dbStations-> get ('stationDetails')'이 같은 테이블을 쿼리하고 있다고 가정합니까? – birderic

답변

1

모든 것을 한 번에 시도 할 수 있습니다. 이 쿼리는 테이블을 아는 것이 가능한 범위에 따라 반경이 $rad 인 역을 제공해야합니다.

변수 $lat1$lng1을 정의해야합니다.

$query= "select country, _id, lat, lng, admin_level_1, locale, 
    (acos(sin(radians($lat1)) * sin(radians(lat)) + cos(radians($lat1)) * 
    cos(radians(lat)) * cos(radians($lng1) - radians(lng))) * 6378) 
    as distance 
    from stationDetails 
    where (acos(sin(radians($lat1)) * sin(radians(lat)) + 
      cos(radians($lat1)) * cos(radians(lat)) * cos(radians($lng1) - 
      radians(lng))) * 6378)<=$rad 
    order by distance desc"; 

echo json_encode($this->db->query($query)->result_array()); 

참고 : 거리는 미터입니다.

+0

답장을 보내 주셔서 감사합니다. 쿼리로 사용했으며 빠르고 효과적입니다. 이것이 제가 찾고 있던 것입니다. MySQL 쿼리에서 이러한 명령을 사용할 수 있는지 몰랐습니다. –

+0

MySQL은 거의 사용하지 않는 기능이 풍부합니다. [here] (http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html)을 더 읽을 수 있습니다 ... –

1

$dbStations->get('stDetails')$dbStations->get('stationDetails')가 동일한 테이블을 쿼리하는 것을 가정 할 때, 당신이 뭔가를 할 수 있습니다 :

$dbStations->select('country, _id, lat, lng, admin_level_1, locale'); 
$stations = $dbStations->get('stDetails'); 

$output = array(); 

foreach ($stations->result() as $station) { 
    $lat2 = $station->lat; 
    $lng2 = $station->lng; 

    $distance = $this->distance->gpsdistance($lat1, $lng1, $lat2, $lng2, "M"); 

    if ($distance <= $rad) { 
     $station->distance = $distance; 
     $output[] = $station; 
    } 
} 

echo json_encode($output); 

나는이 테스트를하지 않은 당신을 포기하지 않을 수 있습니다 당신은 그래서 당신은 할 수 있습니다 원하는 정확히 그것을 조금 꼬집어 라. 중요한 것은 내가 어떻게하는지. 처음에는 모든 데이터를 가져 와서 각 행의 유효성을 검사하십시오. 행이 유효하면 (특정 거리 내에서) 배열에 거리를 더한 후 출력하여 저장하십시오. 행이 남아 있지 않으면 유효한 스테이션을 출력하십시오.

+0

이 코드를 시도 할 때이 오류가 발생합니다 : 치명적인 오류 : stdClass 타입의 객체를 배열로 사용할 수 없다. –

+0

'$ station [ 'distance'] = $ distance'을'$ station-> distance = $ distance'로 변경하십시오. – birderic

+0

그러나 이것은 작동하지만 모든 데이터가 PHP로 오류를 일으키는 메모리를 오버로드합니다. 치명적인 오류 : 메모리가 부족합니다 (45088768 할당) (32 바이트 할당 시도). 나는 PHP 파일을 변경했으나 큰 버퍼로 유지하고 싶지 않습니다. 또한이 메서드 호출은 느립니다. @Alfonso Rubalcava 메소드가 더 길다는 것을 더 좋아합니다. 긴 쿼리 인 LOL입니다. –

관련 문제