0

It is about my site, it is a ad portal and 3 geodata are installed in the system: Germany, Switzerland and Austria.

When I look for an advertisement in Germany, everything works correctly, I'm looking for zip code 68259 and a radius of 30 km. The results are correct, it shows all ads from 68259 Mannheim and the radius of 30 km.

Problem: The problem exists when I search in Switzerland or Austria: I search for the postal code 6000 Lucerne 1 PF and a radius of 30 km ... the results are wrong, I also find ads from Munich or Frankfurt which correspond to 300-500 km radius! I think the mistake is somewhere here in the GEO Post code ! Any Help?

    $this->search_code    = htmlspecialchars($key);
    $this->search_radius  = intval($_GET['search-area']);

    // Germany Postcode
    preg_match('/\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b/is', $this->search_code, $output);
    
    if(!empty($output[0])){
        $this->search_code = $output[0];
    }else{
        // Switzerland, Austria Postcode
        preg_match('/\d{4}/', $this->search_code, $at_ch);
        if(!empty($at_ch[0])){
            $this->search_code = $at_ch[0];
        }
    }

    if( is_numeric($this->search_code) ){
        $this->dbValue = 'geo_plz'; }else{
        $this->dbValue = 'geo_ort'; }

    //query->build
    $get = $this->db->db->query("
        SELECT  geo_lg AS lon, geo_plz, geo_bg AS lat
        FROM    geo
        WHERE   ".$this->dbValue." = '".$this->search_code."'
        ");
    $row = $get->fetch();

    $lon = $row['lon'] / 180 * M_PI;
    $lat = $row['lat'] / 180 * M_PI;
    $plz = $row['geo_plz'];

    //radius->query->search
    $query_boot = "SELECT geo_plz AS plz, (".$this->earthRadius." * SQRT(2*(1-cos(RADIANS(geo_bg)) *
             cos(".$lat.") * (sin(RADIANS(geo_lg)) *
             sin(".$lon.") + cos(RADIANS(geo_lg)) *
             cos(".$lon.")) - sin(RADIANS(geo_bg)) * sin(".$lat.")))) AS Distance
             FROM geo WHERE
             ".$this->earthRadius." * SQRT(2*(1-cos(RADIANS(geo_bg)) *
             cos(".$lat.") * (sin(RADIANS(geo_lg)) *
             sin(".$lon.") + cos(RADIANS(geo_lg)) *
             cos(".$lon.")) - sin(RADIANS(geo_bg)) * sin(".$lat."))) <= ".$this->search_radius."
             ORDER BY Distance
            ";

    $getString = $this->db->db->query($query_boot);
    $rowString = $getString->fetchAll();

    foreach( $rowString as $key => $value ){
        $dataPackageString[] = $value['plz'];
    }

    $resultListener = "";
    if(!empty($dataPackageString)) {
        $listener = join(',', $dataPackageString);
        $listener = str_replace(',', '|', $listener);

        $resultListener = "AND ( postleitzahl REGEXP '$listener|$plz' OR stadt LIKE '%" . $this->search_code . "%' )";
    }else{
        $resultListener = "AND ( postleitzahl = '".$plz."' OR stadt LIKE '%".$this->search_code."%' )";
    }

    return $resultListener;
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • 2
    could it be the regex that you use to match postcodes in Switzerland / Austria? Does this generate / match the postcodes properly? – Professor Abronsius Feb 07 '23 at 11:26
  • I am really a beiginner, bit i suspect it is somewhere from Germany postcode to regex swiss and Austria postcode – Vlado Pranjic Feb 07 '23 at 11:44
  • It generate the postcode good but the search result are wrong ... you can see it live on www.tvojoglas.de – Vlado Pranjic Feb 07 '23 at 11:46
  • One example of test data: If i tip in location 68259 Mannheim and a radius, the result are good, and they are correct for all zip codes in germany. But if i tip zip code 6000 Luzern 1 PF which is in Swiss the results are wrong, and they are wrong forr all other zip codes taht are not like German format! The 4 digit postcode are not validating well – Vlado Pranjic Feb 07 '23 at 13:51
  • Link of example: https://tvojoglas.de/listsearch?id2=&pricestart=&room=&flache=&price=&zulassung=&kilometer=&kraftstoff=&id3=&lat=47.0833&lng=8.2667&isSearch=1&filter_angebote=1&filter_gesuche=1&filter_privat=1&filter_company=1&q=&id1=0&city=6000%20Luzern%201%20PF&search-area=20&& – Vlado Pranjic Feb 07 '23 at 13:53
  • I think the validation of 4 digit postcode dont working, and the german postcode ar not sattled to max 6 digit...Example: 6825 Capolago (in Swiss) the results show everything that has this 4 numbers in postcode ... whick the system take when the validation is not done ! – Vlado Pranjic Feb 07 '23 at 14:06
  • So - you enter an address / address + postcode, this is then POSTed to `instant.php` which does some processing and generates suggested locations which are added to the DOM using the AJAX callback. Clicking on one of these suggestions invokes Google maps and then the search is done? As the SQL uses lat/lng and radius do you in fact need postcodes at all? – Professor Abronsius Feb 07 '23 at 14:19

0 Answers0