Retrouver par code postal tous les lieux qui sont du même dépratement, de la même région ou de la même ville (mysql, php)
Posted by: Ori Pekelman in php, sqlEN: Imagine you have a google maps application (or anything else that deals with places) with data in France, now often you would want to get places that are in the same area as one chosen by the user. Here is a bit of PHP code to help you with that...
It will help you select all the places from your database that have the same postal code (easy) are in the same city, are in the same department or are in the same region (a bit harder). All of this is extremely French and of no use anywhere else... Don't forget to download and importfrench_cities_zipcodes.zip into your MySQL database...
CAVEAT: This code has not been tested yet... There may be some typos in here...
FR: Vous voulez faire une petite application Google Maps ou n'importe quel code PHP où vous avez besoin de trouver pour un lieu les autres lieux qui sont de la même région, du même département ou de la même ville... Voilà un bout de code qui pourra vous être utile.
N'oubliez pas de télécharger et d'importer la table french_cities_zipcodes.zipdans votre base MySQL
CAVEAT: TCe code n'a pas encore été testé du tout... il peut y avoir de fautes de frappe...
-
/**
-
* get Departments For Region By Postal Code
-
* EN: This function will return all the French deprtments that are in the same region for a given
-
* Postal code or the name of the region if $retournerNomRegion=True is passed
-
* This code is VERY French specific.
-
* FR: Cette fonction retourne tous les départements Français qui sont dans la même region qu'un code postal donné
-
* Ou le nom de la région si la valeur $retournerNomRegion=true lui est passée
-
* <b>USAGE</b>:
-
* <code>
-
* //Exemple:
-
* getDepartementsForRegionByPostalCode('75005');
-
* result:'75','77','78','91','92','93','94','95'
-
*
-
* getDepartementsForRegionByPostalCode('75005');
-
* result: 'Ile de France'
-
* </code>
-
* @param $zipcode string (we need to keep leading zeros) zipcode to search for
-
* @return always returns true
-
* @author ori@af83.com
-
* @since Wed March 19 2007 18:09:09 GMT+0200
-
* @version v 0.01 Wed May 22 2007 18:09:09 GMT+0200
-
* For an up to date version go to http://dev.af83.com
-
*/
-
-
function getDepartementsForRegionByPostalCode($zipCode, $retournerNomRegion=false)
-
-
{
-
);
-
-
foreach($liste_regions as $region => $liste_dep)
-
{
-
{
-
}
-
}
-
}
-
-
/**
-
* get SQL For Search By zipcode
-
* EN: This function will return an sql statement to search for either all places that are in the same postal code,
-
* All places that are in the same town, all places that are in the same deparment or all places that are from the same region
-
* We assume here you have a table "places" that has a column for the french zipcode. Change to meet your needs...
-
* This code is VERY French specific.
-
* To use this you will need to import the table (mysql format) joined to this blog post (french_cities_zipcodes) it contains the
-
* correspondance between cities and zipcodes
-
* FR: Cette fonction retourne une requête SQL pour retrouver par code postal tous les lieux qui sont du même code postal, de la même
-
* Ville du même Déprtement ou de la même Région
-
* Le code prend comme postulat l'existence d'une table "places" qui a une colonne "zipcode" pour le code postal.
-
* Pour l'utiliser vous devez importer la table french_cities_zipcodes jointe à ce billet de blog qui contient la correspondance entre les code postaux et les villes.
-
*
-
* <b>USAGE</b>:
-
* <code>
-
* //Exemple:
-
* getSQLForSearchByzipcode('75005', 'zipcode');
-
* </code>
-
* @param $zipcode string zipcode to search for (we need to keep leading zeros)
-
* @return String Sql Statement
-
* @author ori@af83.com
-
* @since Wed March 19 2007 18:09:09 GMT+0200
-
* @version v 0.01 Wed May 22 2007 18:09:09 GMT+0200
-
* For an up to date version go to http://dev.af83.com
-
*/
-
-
function $getSQLForSearchByzipcode($zipcode,$resultFrom){
-
$sqlSelect=" SELECT * from places as p";
-
switch ($resultFrom)
-
{ case 'zipcode': //places from the same zipcode
-
$sqlWhere=" WHERE zipcode LIKE '".$zipcode."' ";
-
break;
-
case 'region': //places from the same region
-
$zipSearch= getDepartementsForRegionByPostalCode ($zipcode);
-
$sqlWhere=" WHERE LEFT(p.zipcode,2) in (".$zipSearch.") ";
-
break;
-
case 'departement': // in France all zipcodes from the same department start with the same two numbers which is the deprtment identifier
-
$sqlWhere=" WHERE LEFT(p.zipcode,2) = $zipSearch ";
-
break;
-
case 'city': // for this you will need a table containing the correspondance between cities and postal codes in your database
-
$sqlSelect=" SELECT * from places as p left join `french_cities_zipcodes` as fcz on p.zipcode =fcz.`zipcode` left join `french_cities_zipcodes` as fcz2 on fcz.city=fcz2.city and fcz.departement=fcz2.departement ";
-
$sqlWhere=" WHERE fcz2.zipcode='$zipcode' ";
-
break;
-
}
-
}
-
return $sqlSelect.$sqlWhere;
-
}

Entries (RSS)
June 1st, 2007 at 1:17 pm
[…] Retrouver par code postal tous les lieux qui sont du même dépratement, de la même région ou de l… […]
June 6th, 2007 at 7:15 pm
I can spoil this one !
You’ll have some issues with Corsican people, Corsica is a region which is split into 2A and 2B departments. And you don’t want to mess with Corsican people !
The DOM and TOM are also very different issues and they won’t appreciate being mixed into the same pseudo-department.
And lust but not least, Bearstech has developped a complete and very bureaucratic conformant French geocoding database for a real eastate PHP site which does automatic demand+offer matching. It has a complete region, department and city list (a hefty 35,000 records if I remember) and redundant data for very efficient ane complete lookups.
It’s bound to be released as GPL, anyone to help for the packaging effort ?
For authorized personnel: http://teddy.bearstech.com/viewcvs/demandimmo/trunk/demandimmo.fr/include/geo.inc.php?rev=157&view=markup
June 8th, 2007 at 6:37 pm
Will study the Corsican, DOM/TOM questions to get better definition there.. but If you have already some Bearcode I will be happy to help package this for release…
June 12th, 2007 at 1:24 pm
I finally took the time to extract the PHP code and release a proper SQL dump. It’s all GPL’ed and I’m handling it to Ori right now.
June 21st, 2007 at 2:30 pm
[…] Retrouver par code postal tous les lieux qui sont du même dépratement, de la même région ou de l… […]