json encode + retour

Par flo3376, il y a 12 ans


bonjour à tous

je me tente au json, mais pas sans mal

voici 2 programme

<?php
try{
        $DBPDO = new PDO($dbdns1,$dbuser1 , $dbpass1,
                    array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
        $DBPDO-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    try{
            $sql="SELECT a.*, c.name as aircraftname,p.code, p.pilotid as pilotid, p.firstname, p.lastname,dep.name as depname, dep.lat AS deplat, dep.lng AS deplng,arr.name as arrname, arr.lat AS arrlat, arr.lng AS arrlng
                FROM tracker_cil_acarsdata a
                LEFT JOIN tracker_cil_aircraft c ON a.`aircraft`= c.`registration`
                LEFT JOIN tracker_cil_pilots p ON a.`pilotid`= p.`pilotid`
                LEFT JOIN tracker_cil_airports AS dep ON dep.icao = a.depicao
                LEFT JOIN tracker_cil_airports AS arr ON arr.icao = a.arricao ";
            $req = $DBPDO ->query($sql);

            while($flight=$req->fetch(PDO::FETCH_OBJ)) 
                {

                    $data]=$flight;
                }
                //var_dump($data)   ;
                json_encode($data);  

        }
    catch (PDOException $e) {
            echo "merde req";
            }

            }
catch (PDOException $e){
        echo "merde bd";
        }        
?>

function liveRefresh()
{
    $.ajax({
        type: "GET",
        url: "./site/membre/pages/home/demande.php",
        dataType: "json",
        cache: false,
        success: function(data) 
        {
            alertObject(data);
            populateMap(data);

        }
    });
alert('test');
};
function alertObject(obj){      
        for(var key in obj) {
        alert('key: ' + key + '\n' + 'value: ' + obj[key]);
        if( typeof obj[key] === 'object' ) {
            alertObject(obj[key]);
        }
        }
    }
var getObject = {};
// object set with key an val
getObject.swfVersionStr = '10.0';
getObject.xiSwfUrlStr = null;
getObject.flashvarsObj = {};
getObject.parObj = {allowfullscreen: "true",wmode: "window",menu: "false"};

quand je lance la page concerné, j'ai bien l'alert test, donc mon code s'exécute mais mon alertObject(data); n'arrive pas, donc je suppose que mon ajax à foirée car mon json n'est pas fait ou mal fait.

je sais que j'ai des données, mon vardump me retourne ça

array(7) { [0]=> object(stdClass)#3 (35) { "id"]=> string(2) "46" "pilotid"]=> string(1) "1" "flightnum"]=> string(4) "test" "pilotname"]=> string(10) "FFS2 FL-76" "aircraft"]=> string(6) "B747-3" "lat"]=> string(15) "50.458131710493" "lng"]=> string(15) "7.3443200780326" "heading"]=> string(2) "60" "alt"]=> string(5) "20000" "gs"]=> string(3) "435" "depicao"]=> string(4) "LFPO" "depapt"]=> string(10) "PARIS ORLY" "arricao"]=> string(4) "LFPO" "arrapt"]=> string(10) "PARIS ORLY" "deptime"]=> string(8) "19:51:00" "timeremaining"]=> string(4) "0:30" "arrtime"]=> string(8) "20:24:16" "route"]=> string(0) "" "route_details"]=> string(0) "" "distremain"]=> string(3) "220" "phasedetail"]=> string(10) "Croisière" "online"]=> string(0) "" "messagelog"]=> string(0) "" "lastupdate"]=> string(19) "2014-02-02 20:24:16" "client"]=> string(6) "kACARS" "aircraftname"]=> string(4) "B747" "code"]=> string(3) "FFS" "firstname"]=> string(4) "FFS2" "lastname"]=> string(5) "FL-76" "depname"]=> string(10) "PARIS ORLY" "deplat"]=> string(7) "48.7233" "deplng"]=> string(6) "2.3794" "arrname"]=> string(10) "PARIS ORLY" "arrlat"]=> string(7) "48.7233" "arrlng"]=> string(6) "2.3794" } [1]=> object(stdClass)#4 (35) { "id"]=> string(2) "47" "pilotid"]=> string(1) "1" "flightnum"]=> string(4) "test" "pilotname"]=> string(10) "FFS2 FL-76" "aircraft"]=> string(6) "B747-3" "lat"]=> string(15) "49.634693242213" "lng"]=> string(15) "4.9442370604270" "heading"]=> string(2) "60" "alt"]=> string(5) "19997" "gs"]=> string(3) "437" "depicao"]=> string(4) "LFPO" "depapt"]=> string(10) "PARIS ORLY" "arricao"]=> string(4) "LFPO" "arrapt"]=> string(10) "PARIS ORLY" "deptime"]=> string(8) "19:51:00" "timeremaining"]=> string(4) "0:16" "arrtime"]=> string(8) "20:09:52" "route"]=> string(0) "" "route_details"]=> string(0) "" "distremain"]=> string(3) "115" "phasedetail"]=> string(10) "Croisière" "online"]=> string(0) "" "messagelog"]=> string(0) "" "lastupdate"]=> string(19) "2014-02-02 20:09:52" "client"]=> string(6) "kACARS" "aircraftname"]=> string(4) "B747" "code"]=> string(3) "FFS" "firstname"]=> string(4) "FFS2" "lastname"]=> string(5) "FL-76" "depname"]=> string(10) "PARIS ORLY" "deplat"]=> string(7) "48.7233" "deplng"]=> string(6) "2.3794" "arrname"]=> string(10) "PARIS ORLY" "arrlat"]=> string(7) "48.7233" "arrlng"]=> string(6) "2.3794" } [2]=> object(stdClass)#5 (35) { "id"]=> string(2) "48" "pilotid"]=> string(1) "1" "flightnum"]=> string(4) "test" "pilotname"]=> string(10) "FFS2 FL-76" "aircraft"]=> string(6) "B747-3" "lat"]=> string(15) "49.634693242213" "lng"]=> string(15) "4.9442370604270" "heading"]=> string(2) "60" "alt"]=> string(5) "19997" "gs"]=> string(3) "437" "depicao"]=> string(4) "LFPO" "depapt"]=> string(10) "PARIS ORLY" "arricao"]=> string(4) "LFPO" "arrapt"]=> string(10) "PARIS ORLY" "deptime"]=> string(8) "19:51:00" "timeremaining"]=> string(4) "0:16" "arrtime"]=> string(8) "20:09:52" "route"]=> string(0) "" "route_details"]=> string(0) "" "distremain"]=> string(3) "115" "phasedetail"]=> string(10) "Croisière" "online"]=> string(0) "" "messagelog"]=> string(0) "" "lastupdate"]=> string(19) "2014-02-02 20:09:52" "client"]=> string(6) "kACARS" "aircraftname"]=> string(4) "B747" "code"]=> string(3) "FFS" "firstname"]=> string(4) "FFS2" "lastname"]=> string(5) "FL-76" "depname"]=> string(10) "PARIS ORLY" "deplat"]=> string(7) "48.7233" "deplng"]=> string(6) "2.3794" "arrname"]=> string(10) "PARIS ORLY" "arrlat"]=> string(7) "48.7233" "arrlng"]=> string(6) "2.3794" } [3]=> object(stdClass)#6 (35) { "id"]=> string(2) "49" "pilotid"]=> string(1) "1" "flightnum"]=> string(4) "test" "pilotname"]=> string(10) "FFS2 FL-76" "aircraft"]=> string(6) "B747-3" "lat"]=> string(15) "49.634693242213" "lng"]=> string(15) "4.9442370604270" "heading"]=> string(2) "60" "alt"]=> string(5) "19997" "gs"]=> string(3) "437" "depicao"]=> string(4) "LFPO" "depapt"]=> string(10) "PARIS ORLY" "arricao"]=> string(4) "LFPO" "arrapt"]=> string(10) "PARIS ORLY" "deptime"]=> string(8) "19:51:00" "timeremaining"]=> string(4) "0:16" "arrtime"]=> string(8) "20:09:52" "route"]=> string(0) "" "route_details"]=> string(0) "" "distremain"]=> string(3) "115" "phasedetail"]=> string(10) "Croisière" "online"]=> string(0) "" "messagelog"]=> string(0) "" "lastupdate"]=> string(19) "2014-02-02 20:09:52" "client"]=> string(6) "kACARS" "aircraftname"]=> string(4) "B747" "code"]=> string(3) "FFS" "firstname"]=> string(4) "FFS2" "lastname"]=> string(5) "FL-76" "depname"]=> string(10) "PARIS ORLY" "deplat"]=> string(7) "48.7233" "deplng"]=> string(6) "2.3794" "arrname"]=> string(10) "PARIS ORLY" "arrlat"]=> string(7) "48.7233" "arrlng"]=> string(6) "2.3794" } [4]=> object(stdClass)#7 (35) { "id"]=> string(2) "50" "pilotid"]=> string(1) "1" "flightnum"]=> string(4) "test" "pilotname"]=> string(10) "FFS2 FL-76" "aircraft"]=> string(6) "B747-3" "lat"]=> string(15) "49.634693242213" "lng"]=> string(15) "4.9442370604270" "heading"]=> string(2) "60" "alt"]=> string(5) "19997" "gs"]=> string(3) "437" "depicao"]=> string(4) "LFPO" "depapt"]=> string(10) "PARIS ORLY" "arricao"]=> string(4) "LFPO" "arrapt"]=> string(10) "PARIS ORLY" "deptime"]=> string(8) "19:51:00" "timeremaining"]=> string(4) "0:16" "arrtime"]=> string(8) "20:09:52" "route"]=> string(0) "" "route_details"]=> string(0) "" "distremain"]=> string(3) "115" "phasedetail"]=> string(10) "Croisière" "online"]=> string(0) "" "messagelog"]=> string(0) "" "lastupdate"]=> string(19) "2014-02-02 20:09:52" "client"]=> string(6) "kACARS" "aircraftname"]=> string(4) "B747" "code"]=> string(3) "FFS" "firstname"]=> string(4) "FFS2" "lastname"]=> string(5) "FL-76" "depname"]=> string(10) "PARIS ORLY" "deplat"]=> string(7) "48.7233" "deplng"]=> string(6) "2.3794" "arrname"]=> string(10) "PARIS ORLY" "arrlat"]=> string(7) "48.7233" "arrlng"]=> string(6) "2.3794" } [5]=> object(stdClass)#8 (35) { "id"]=> string(2) "51" "pilotid"]=> string(1) "1" "flightnum"]=> string(4) "test" "pilotname"]=> string(10) "FFS2 FL-76" "aircraft"]=> string(6) "B747-3" "lat"]=> string(15) "49.634693242213" "lng"]=> string(15) "4.9442370604270" "heading"]=> string(2) "60" "alt"]=> string(5) "19997" "gs"]=> string(3) "437" "depicao"]=> string(4) "LFPO" "depapt"]=> string(10) "PARIS ORLY" "arricao"]=> string(4) "LFPO" "arrapt"]=> string(10) "PARIS ORLY" "deptime"]=> string(8) "19:51:00" "timeremaining"]=> string(4) "0:16" "arrtime"]=> string(8) "20:09:52" "route"]=> string(0) "" "route_details"]=> string(0) "" "distremain"]=> string(3) "115" "phasedetail"]=> string(10) "Croisière" "online"]=> string(0) "" "messagelog"]=> string(0) "" "lastupdate"]=> string(19) "2014-02-02 20:09:52" "client"]=> string(6) "kACARS" "aircraftname"]=> string(4) "B747" "code"]=> string(3) "FFS" "firstname"]=> string(4) "FFS2" "lastname"]=> string(5) "FL-76" "depname"]=> string(10) "PARIS ORLY" "deplat"]=> string(7) "48.7233" "deplng"]=> string(6) "2.3794" "arrname"]=> string(10) "PARIS ORLY" "arrlat"]=> string(7) "48.7233" "arrlng"]=> string(6) "2.3794" } [6]=> object(stdClass)#9 (35) { "id"]=> string(2) "52" "pilotid"]=> string(1) "1" "flightnum"]=> string(4) "test" "pilotname"]=> string(10) "FFS2 FL-76" "aircraft"]=> string(6) "B747-3" "lat"]=> string(15) "49.634693242213" "lng"]=> string(15) "4.9442370604270" "heading"]=> string(2) "60" "alt"]=> string(5) "19997" "gs"]=> string(3) "437" "depicao"]=> string(4) "LFPO" "depapt"]=> string(10) "PARIS ORLY" "arricao"]=> string(4) "LFPO" "arrapt"]=> string(10) "PARIS ORLY" "deptime"]=> string(8) "19:51:00" "timeremaining"]=> string(4) "0:16" "arrtime"]=> string(8) "20:09:52" "route"]=> string(0) "" "route_details"]=> string(0) "" "distremain"]=> string(3) "115" "phasedetail"]=> string(10) "Croisière" "online"]=> string(0) "" "messagelog"]=> string(0) "" "lastupdate"]=> string(19) "2014-02-02 20:09:52" "client"]=> string(6) "kACARS" "aircraftname"]=> string(4) "B747" "code"]=> string(3) "FFS" "firstname"]=> string(4) "FFS2" "lastname"]=> string(5) "FL-76" "depname"]=> string(10) "PARIS ORLY" "deplat"]=> string(7) "48.7233" "deplng"]=> string(6) "2.3794" "arrname"]=> string(10) "PARIS ORLY" "arrlat"]=> string(7) "48.7233" "arrlng"]=> string(6) "2.3794" } }

que puis je faire pour m'en sortir.

merci d'avance flo.

4 réponses

MrGuillou, il y a 12 ans

Bonsoir,

echo json_encode(data) et pas simple json\_encode(data);

json_encode retourne une chaîne de caractère de caractère, mais ne l'affiche pas.

Cordialement,

flo3376, il y a 12 ans

oups, je test mais bon, dans le genre boulet là je dois me féliciter

MrGuillou, il y a 12 ans

Meme code que toi, mais en plus propre

try{
    $DBPDO = new PDO($dbdns1,$dbuser1 , $dbpass1, 
            array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    $DBPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e){
    echo json_encode(array('error' => 'impossible de se connecter à la base de donnée'));
    exit;
}
try{
    $sql="SELECT a.*, c.name as aircraftname,p.code, p.pilotid as pilotid, p.firstname, p.lastname,dep.name as depname, dep.lat AS deplat, dep.lng AS deplng,arr.name as arrname, arr.lat AS arrlat, arr.lng AS arrlng
        FROM tracker_cil_acarsdata a
        LEFT JOIN tracker_cil_aircraft c ON a.`aircraft`= c.`registration`
        LEFT JOIN tracker_cil_pilots p ON a.`pilotid`= p.`pilotid`
        LEFT JOIN tracker_cil_airports AS dep ON dep.icao = a.depicao
        LEFT JOIN tracker_cil_airports AS arr ON arr.icao = a.arricao ";
    $req = $DBPDO->query($sql);
    $data = $req->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($data);
}
    catch (PDOException $e) {
    echo json_encode(array('error' => 'impossible de récupérer les données dans la base de donnée'));
    exit;
}

Donc pas besoin du while.
En plus tu peux intercepter l'erreur coté javascript.

Cordialement,

flo3376, il y a 12 ans

merci beaucoup