【问题标题】:Search on the bases of Postcode and Distance to search a Categories根据邮政编码和距离搜索以搜索类别
【发布时间】:2013-01-02 10:08:58
【问题描述】:

我正在根据邮政编码和距离进行搜索以搜索类别。我想要该邮政编码的 10 英里无线电中特定类别的结果。

我的表格格式是

CREATE TABLE IF NOT EXISTS `uk_data` (
  `slno` int(10) NOT NULL AUTO_INCREMENT,
  `comp_name` varchar(150) DEFAULT NULL,
  `comp_no` varchar(50) DEFAULT NULL,
  `comp_street` varchar(100) DEFAULT NULL,
  `comp_area` varchar(100) DEFAULT NULL,
  `comp_post_code` varchar(15) DEFAULT NULL,
  `comp_phone` varchar(100) DEFAULT NULL,
  `comp_phone1` varchar(100) DEFAULT NULL,
  `cat1` varchar(100) DEFAULT NULL,
  `cat2` varchar(100) DEFAULT NULL,
  `cat3` varchar(100) DEFAULT NULL,
  `cat4` varchar(100) DEFAULT NULL,
  `cat5` varchar(100) DEFAULT NULL,
  `cat6` varchar(100) DEFAULT NULL,
  `cat7` varchar(100) DEFAULT NULL,
  `cat8` decimal(9,6) DEFAULT NULL,
  `cat9` decimal(9,6) DEFAULT NULL,
  `cat10` varchar(15) DEFAULT NULL,
  PRIMARY KEY (`slno`),
  UNIQUE KEY `Phone` (`comp_phone`),
  KEY `cat10` (`cat10`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=31717 ;

现在

cat10 is postcode

cat8 is Latitude

cat9 is Longitude

我能够计算距离并在纬度和经度的特定英里范围内得到结果

SELECT cat10, ( 3959 * acos( cos( radians( {$coords['Latitude']} ) ) 
* cos( radians( cat8 ) ) * cos( radians( cat9 ) - radians( {$coords['Longitude']} ) ) 
+ sin( radians( {$coords['Latitude']} ) ) * sin( radians( cat8 ) ) ) ) 
AS distance FROM uk_data 
HAVING distance <= {$radius} ORDER BY distance

$coords = array('Latitude' => "57.149727", 'Longitude' => "-2.094735");
$radius = .5;    
$uk_data_radious = uk_data_radious_test($coords,$radius,$q,$pc);

现在我正在尝试进行完整搜索以获取具有特定邮政编码距离的类别..

SELECT *, ( 3959 * acos( cos( radians( {$coords['Latitude']} ) ) * cos( radians( cat8 ) ) * cos( radians( cat9 ) - radians( {$coords['Longitude']} ) ) + sin( radians( {$coords['Latitude']} ) ) * sin( radians( cat8 ) ) ) ) AS distance 
    FROM uk_data where
        cat1 like :cat OR
        cat2 like :cat OR
        cat3 like :cat OR
        cat4 like :cat OR
        cat5 like :cat OR
        cat6 like :cat OR
        cat7 like :cat
    HAVING distance <= {$radius} 
    ORDER BY distance

但这不起作用。我知道 Query 中有错误,但不知道如何使用 HAVING 和 Where 一起处理 3 个参数 Postcode Distance and categories

错误

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error
 or access violation: 1064 You have an error in your SQL syntax; check the manual that
 corresponds to your MySQL server version for the right syntax to use near ':cat OR cat2 
like :cat OR cat3 like :cat OR cat4 like :cat OR ' at line 4' in
 D:\Winginx\home\****\public_html\functions.php:365 Stack trace: #0 
D:\Winginx\home\****\public_html\functions.php(365): PDO->query('SELECT *, ( 395...') #1 
D:\Winginx\home\***\public_html\miles_output.php(14): uk_data_radious_test(Array, 0.5, 
'Tool') #2 {main} thrown in D:\Winginx\home\***\public_html\functions.php on line 365 

【问题讨论】:

    标签: php mysql distance latitude-longitude


    【解决方案1】:

    尝试输出错误信息。

    我不确定,但我认为您不需要使用 HAVING,只需在 ( OR .. ) 部分周围使用大括号,如下所示:

    SELECT *, ( 3959 * acos( cos( radians( {$coords['Latitude']} ) ) * cos( radians( cat8 ) ) * cos( radians( cat9 ) - radians( {$coords['Longitude']} ) ) + sin( radians( {$coords['Latitude']} ) ) * sin( radians( cat8 ) ) ) ) AS distance 
    FROM uk_data where
       (
        cat1 like :cat OR
        cat2 like :cat OR
        cat3 like :cat OR
        cat4 like :cat OR
        cat5 like :cat OR
        cat6 like :cat OR
        cat7 like :cat
       ) AND distance <= {$radius} 
    ORDER BY distance
    

    附带说明一下,每个类别都有一个包含列的表格是不好的设计;每次添加新类别时,您都需要更改 sql 表结构。

    【讨论】:

    【解决方案2】:

    我终于为我的问题找到了解决方案......但我认为可能有更好的方法来做到这一点......我仍然想在这里分享答案,所以如果有人正在寻找相同的答案可能会得到帮助,是的,如果有人有更好的方法来做到这一点,请建议 thx

    我在做什么

    第一个make函数

    function distance($lat1, $lon1, $lat2, $lon2, $unit) { //** this to calculate distance in Miles or Km
    
          $theta = $lon1 - $lon2; 
          $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
          $dist = acos($dist); 
          $dist = rad2deg($dist); 
          $miles = $dist * 60 * 1.1515;
          $unit = strtoupper($unit);
    
          if ($unit == "K") {
            return ($miles * 1.609344); 
          } else if ($unit == "N") {
              return ($miles * 0.8684);
            } else {
                return $miles;
              }
        }
    
    
            function uk_data_radious($coords, $radius){//** this to get result within certain radios for a postcode
            global $DBH;
            $STH = $DBH->prepare("SELECT DISTINCT cat10, ( 3959 * acos( cos( radians({$coords['Latitude']}) ) 
                      * cos( radians( cat8 ) ) 
                      * cos( radians( cat9 ) - radians({$coords['Longitude']}) ) 
                      + sin( radians({$coords['Latitude']}) ) 
                      * sin( radians( cat8 ) ) )) AS distance FROM uk_data HAVING distance <= {$radius} ORDER BY distance");
            $STH->bindValue(':postcode', "$postcode", PDO::PARAM_STR);
            $STH->setFetchMode(PDO::FETCH_ASSOC);
            $STH->execute();
            return $STH;
        }
    
            function uk_data_pc_count($user_count){//*** this is to check if post code is in our database or not 
            global $DBH;
            $STH = $DBH->prepare("SELECT * from uk_data where cat10 = :user_count");
            $STH->bindValue(':user_count', $user_count, PDO::PARAM_STR);
            $STH->execute();
            $row_count = $STH ->rowCount();
            return $row_count ;
        }
    
    function comp_post_code($cat, $comp_post_code){//** this to get result within postcode
        global $DBH;
        $STH = $DBH->prepare("SELECT * from uk_data where 
                                cat10 like :comp_post_code  AND (
                                cat1 like :cat OR
                                cat2 like :cat OR
                                cat3 like :cat OR
                                cat4 like :cat OR
                                cat5 like :cat OR
                                cat6 like :cat OR
                                cat7 like :cat OR
                                cat8 like :cat OR
                                cat9 like :cat 
                                )") ;
        $STH->bindValue(':cat', "%$cat%", PDO::PARAM_STR);
        $STH->bindValue(':comp_post_code', "$comp_post_code%", PDO::PARAM_STR);
        $STH->execute();
        $STH->setFetchMode(PDO::FETCH_ASSOC);
        return $STH;
        }
    

    现在在页面上使用它

    $q = $_GET['id'];
    $pc =  change_text(' ', '',$_GET['pc']);
    $qu = change_text(' ', '%', $q);
    $pc_o = array();
    $comp_name = array();
    $comp_phone = array();
    $distance = array();
    $Latitude = array();
    $Longitude = array();
    $post_code = array();
    
    if(uk_data_pc_count($pc) > 0  ){
    
    $uk_data_ll = uk_data_ll($pc);
    while (($row = $uk_data_ll->fetch()) !== false) {
    $Latitude[] = $row['cat8'];
    $Longitude[] = $row['cat9'];
    
    }
    
    $coords = array('Latitude' => "$Latitude[0]", 'Longitude' => "$Longitude[0]");
    $radius = 10;
    
    $uk_data_radious = uk_data_radious($coords,$radius,$q);
    while (($row = $uk_data_radious->fetch()) !== false) {
        /*echo $row['cat10'].' --- '.$row['distance'].'<br>';*/
        $pc_o[] = $row['cat10'];
        $distance[] = $row['distance'];
    
    }
    foreach($pc_o as $pc_o){
    $uk_data_radious = comp_post_code($q,$pc_o);
    while (($row = $uk_data_radious->fetch()) !== false) {
    $comp_name[] = $row['comp_name'];
    $comp_phone[] = $row['comp_phone'];
    $post_code[] = $row['cat10'];
    $distance_m[] = distance($Latitude[0],$Longitude[0],$row['cat8'],$row['cat9'],"M");
    }}
    
    
    
    for($i=0, $count = count($comp_name);$i<$count;$i++) {
        echo $post_code[$i].' - '.$comp_name[$i].' - '.$comp_phone[$i].' - '.round($distance_m[$i],2).' Miles<br>';
        }
    }else {echo 'Wrong Post-Code Selected';}
    

    thx 希望这会有所帮助,并希望得到好的建议;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多