diff --git a/ground/controllers/PageController.php b/ground/controllers/PageController.php index e3afa6b..9604783 100644 --- a/ground/controllers/PageController.php +++ b/ground/controllers/PageController.php @@ -70,11 +70,13 @@ class PageController extends Controller $randomPoint = Point::model()->getRandom(); $similarPoints = Point::model()->getAll(array('where' => 'categoryId = '.$point->categoryId.' AND `id` <> '.$point->id.' ORDER BY RAND() LIMIT 6;')); + $closestPoints = Point::model()->getClosestPoints($id); self::render('point.php', array( 'point' => $point, 'randomPoint' => $randomPoint, 'similarPoints' => $similarPoints, + 'closestPoints' => $closestPoints, 'categories' => $categories, 'photos' => $point->photos, )); diff --git a/ground/models/Point.php b/ground/models/Point.php index a941300..adc0468 100644 --- a/ground/models/Point.php +++ b/ground/models/Point.php @@ -44,6 +44,40 @@ class Point extends Model return $this->getByPK($pointId); } + public function getClosestPoints($pointId) + { + $point = Point::model()->getByPK($pointId); + $lat = floatval($point->lat); + $lng = floatval($point->lng); + + $query = ' + SELECT *, ( + SQRT(POW(`lat`-:lat,2)+POW(`lng`-:lng,2)) + ) AS `dist` FROM `' . $this->_tableName_ . '` + WHERE + `lat` BETWEEN :minLat AND :maxLat + AND + `lng` BETWEEN :minLng AND :maxLng + AND + `lat` <> :lat + AND + `lng` <> :lng + ORDER BY `dist` + LIMIT 6;'; + + $res = App::DB()->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); + $res->execute(array(':lat' => $lat, ':lng' => $lng, ':minLat' => $lat-1, ':minLng' => $lng-1, ':maxLat' => $lat+1, ':maxLng' => $lng+1)); + + $ready = array(); + $calledClass = get_called_class(); + while ($res && $row = $res->fetch(PDO::FETCH_ASSOC)) + { + $ready[] = new $calledClass($row); + } + + return $ready; + } + public function getCountByUser($userId) { $res = App::DB()->query('SELECT count(`id`) AS `cnt` FROM ' . $this->_tableName_ . ' WHERE `author` = ' . $userId . ' ;')->fetch(PDO::FETCH_ASSOC); diff --git a/ground/views/point.php b/ground/views/point.php index 901bd27..ca70a3e 100644 --- a/ground/views/point.php +++ b/ground/views/point.php @@ -98,9 +98,13 @@

Поблизости:

-

-

-

+ + Известных нам точек поблизости нет. + + + + +

Похожие: