AutoComplétion Code Postal/Ville
Bonjour à tous :)
j'essais d'installer le script "AutoComplétion Code Postal/Ville avec jQuery"...
mais coté PHP je ne programme pas en PDO, de fait le fichier AutoCompletion.php ne fonctionne pas ....
J'essai donc de le réécrire en PHP/MySQL classique, mais le tableau $list
retourne
Code:
1 2 3
| 0: "{CodePostal:63000, Ville:Clermont-Ferrand}"
1: "{CodePostal:63000, Ville:Clermont-Ferrand}"
2: "{CodePostal:63001, Ville:Clermont-Ferrand}" |
au lieu de
Code:
1 2 3
| 0: {CodePostal:63000, Ville:Clermont-Ferrand}
1: {CodePostal:63001, Ville:Clermont-Ferrand}
2: {CodePostal:63002, Ville:Clermont-Ferrand} |
de fait ca BUG :(
quelqu'un pourrait-il me dire comment faire, SVP ?
MERCI DE VOTRE AIDE !! :)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| require_once('./config.inc.php');
//Initialisation de la liste
$list = array();
//Construction de la requete
$strQuery = "SELECT CP AS CodePostal, VILLE AS Ville FROM cp_autocomplete WHERE ";
if (isset($_POST["codePostal"])){
$strQuery .= "CP LIKE '".mysql_real_escape_string($_POST['codePostal'])."%' ";
}
else {
$strQuery .= "VILLE LIKE '".mysql_real_escape_string($_POST['ville'])."%' ";
}
$strQuery .= "AND CODEPAYS = 'FR' ";
//Limite
if (isset($_POST["maxRows"])){
$strQuery .= "LIMIT 0,".mysql_real_escape_string($_POST['maxRows'])."";
}
$query = mysql_query($strQuery);
$i=0 ;
while ($enr = mysql_fetch_array($query)) {
$list[$i] = "{CodePostal:".$enr['CodePostal'].", Ville:".addslashes($enr['Ville'])."}" ;
$i++;
} |
le code initial étant
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| require_once('./AutoCompletionCPVille.class.php');
//Initialisation de la liste
$list = array();
//Connexion MySQL
try
{
$db = new PDO('mysql:host=localhost;dbname=bdname', 'root', 'root');
}
catch (Exception $ex)
{
echo $ex->getMessage();
}
//Construction de la requete
$strQuery = "SELECT CP CodePostal, VILLE Ville FROM autocomplete WHERE ";
if (isset($_POST["codePostal"]))
{
$strQuery .= "CP LIKE :codePostal ";
}
else
{
$strQuery .= "VILLE LIKE :ville ";
}
$strQuery .= "AND CODEPAYS = 'FR' ";
//Limite
if (isset($_POST["maxRows"]))
{
$strQuery .= "LIMIT 0, :maxRows";
}
$query = $db->prepare($strQuery);
if (isset($_POST["codePostal"]))
{
$value = $_POST["codePostal"]."%";
$query->bindParam(":codePostal", $value, PDO::PARAM_STR);
}
else
{
$value = $_POST["ville"]."%";
$query->bindParam(":ville", $value, PDO::PARAM_STR);
}
//Limite
if (isset($_POST["maxRows"]))
{
$valueRows = intval($_POST["maxRows"]);
$query->bindParam(":maxRows", $valueRows, PDO::PARAM_INT);
}
$query->execute();
$list = $query->fetchAll(PDO::FETCH_CLASS, "AutoCompletionCPVille");;
echo json_encode($list); |
Merci bonne journée