45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
include_once('listeCpVilles.php');
|
|
|
|
// --------------------------------------------------------------------------- //
|
|
// bsearch
|
|
// --------------------------------------------------------------------------- //
|
|
function bsearch(&$tab, $a, $b, $s, $l)
|
|
{
|
|
if ($a > $b) {
|
|
return -1;
|
|
}
|
|
|
|
$m = ($a + $b) >> 1;
|
|
|
|
$c = strcmp($s, substr($tab[$m], 0, $l));
|
|
if ($c == 0) {
|
|
return $m;
|
|
} else if ($c < 0) {
|
|
return bsearch($tab, $a, $m - 1, $s, $l);
|
|
} else {
|
|
return bsearch($tab, $m + 1, $b, $s, $l);
|
|
}
|
|
}
|
|
|
|
// --------------------------------------------------------------------------- //
|
|
// Main
|
|
// --------------------------------------------------------------------------- //
|
|
$completion = array();
|
|
$strlen = strlen($_GET['val']);
|
|
$upper = strtoupper($_GET['val']) ;
|
|
$i = bsearch($cpVilles, 0, sizeof($cpVilles) - 1, $upper, $strlen);
|
|
if ($i >= 0) {
|
|
while ($i > 0 && substr($cpVilles[$i - 1], 0, $strlen) == $upper) {
|
|
--$i;
|
|
}
|
|
while ($i < sizeof($cpVilles) &&
|
|
substr($cpVilles[$i], 0, $strlen) == $upper) {
|
|
$completion[] = '"'.$cpVilles[$i].'"';
|
|
++$i;
|
|
}
|
|
}
|
|
|
|
// Envoi au javascript au format JSON ['valeur1','valeur2', ...]
|
|
print '['.join(',', $completion).']';
|
|
?>
|