如果用户位置与数据位置(经度,纬度)匹配且位于给定半径内,则用户可以查看数据

时间:2019-05-04 06:00:54

标签: php mysql

我正在创建一个基于位置的竞赛,在该竞赛中,用户将与给定半径内的竞赛位置进行位置匹配的竞赛参加。 表格架构

+---------------------+------------+------------+
| User Location Table |            |            |
+---------------------+------------+------------+
| UserId              | lat        | lon        |
| 10                  | 19.2332599 | 73.0849823 |
+---------------------+------------+------------+

+-----------------+---------------------------+---------+---------+
|  Contest Table  |                           |         |         |
+-----------------+---------------------------+---------+---------+
| ID  Title       |   Description             | lat     | lon     |
| 1  Test Contest | Dummy contest description | 17.3850 | 78.4867 |
+-----------------+---------------------------+---------+---------+

+--------------+-----------------+
|  User Table  |                 |
+--------------+-----------------+
| ID  username |   email         |
| 10  Test User | dummy@email.com |
+--------------+-----------------+

以下是我的查询:-

$sql_query  = "select * FROM contest order by id DESC";
  $dbCon  = connect_db();
  $stmt   = $dbCon->query($sql_query);
  while($row = $stmt->fetch_array(MYSQLI_ASSOC))
  {

    $user = "SELECT lat,lon FROM user_location WHERE user_id='".$userId."' and status='active'";
      $dbCon  = connect_db();
      $rs_con1 = $dbCon->query($user);
      $fet_user = $rs_con1->fetch_array(MYSQLI_ASSOC);
      $latitude = $fet_user['lat'];
      $longitude = $fet_user['lon'];
      $distamceforzoom = $row['radius'];
      $sq_l = "SELECT *,( 6371 * acos( cos( radians('$latitude') ) * cos( radians( lat ) ) * cos( radians( `long` ) - radians('$longitude') ) + sin( radians('$latitude') ) * sin( radians( lat ) ) ) ) AS distance
      FROM contest WHERE status='active' HAVING distance <= '$distamceforzoom' AND id='".$row['id']."' ";
      //echo $sq_l;
      $dbCon  = connect_db();
      $rs_l = $dbCon->query($sq_l);
      $row_con1 = $rs_l->num_rows;
      //echo $row_con1;
      if($row_con1>0)
      {
         echo "Contest is present in your location";
      }
     else 
     {
       echo "Contest in not present in your location";
     }
 }

但是此查询无法正常运行,该比赛在其他位置也可见。

0 个答案:

没有答案