extranet/www/pages/nafajax.php
Michael RICOIS 422c1384e7 Code
2010-05-28 15:25:17 +00:00

34 lines
1.1 KiB
PHP

<?php
$q = (!$_GET["q"])? "" : strtolower($_GET["q"]);
if (!$q) return;
//Connection mysql
$link = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or
die("Impossible de se connecter : " . mysql_error());
$db_selected = mysql_select_db(MYSQL_DB, $link);
if (!$db_selected) {
die ('Impossible de sélectionner la base de données : ' . mysql_error());
}
//Lecture des données mysql
$sql = "SET NAMES 'utf8'";
$result = mysql_query($sql);
$sql = "SELECT codNaf5, libNaf5 FROM tabnaf5 WHERE codNaf5 LIKE '%$q%' OR libNaf5 LIKE '%$q%' ORDER BY poids DESC";
$result = mysql_query($sql);
$output = array();
if ( mysql_num_rows($result)>0 && mysql_num_rows($result)!=FALSE ){
while ($row = mysql_fetch_assoc($result)) {
$items[$row['codNaf5']] = "".$row['libNaf5']."";
}
//Envoi des informations
$separator = " , ";
foreach ($items as $key=>$value) {
if (strpos(strtolower($key), $q) !== false ||
strpos(strtolower($value), $q) !== false ) {
$output[] = array(
'label' => $key . $separator . $value,
'value' => $key
);
}
}
}
echo json_encode($output);
?>