2016-10-13 12 views
0

エンジンの状態に基づいてユーザーが行った旅行(ルート)を取得しようとしていて、engine=1がエンジン始まり、engine=2がエンジンオフイベントの日付順に並べ替えると、だから毎回私がengine=2を見つけると、それは旅行の終わりです。私はこの旅行をJSON形式で提示する必要があります。私はPHPからそれをやっていますが、実際の旅行を個別に取得することにはまっています。php while loop stepped

ご協力いただければ幸いです。

mysql> select imei, lat, lon, date, engine from dataGps order by date ; 
+-----------------+-----------+------------+---------------------+--------+ 
| imei   | lat  | lon  | date    | engine | 
+-----------------+-----------+------------+---------------------+--------+ 
| 864251020174384 | 12.292415 | -86.236351 | 2016-10-04 10:53:34 |  2 | 
| 864251020174384 | 12.134856 | -86.251427 | 2016-10-04 14:14:40 |  1 | 
| 864251020174383 | 12.137000 | -86.254501 | 2016-10-04 14:15:21 |  2 | 
| 864251020174383 | 12.135400 | -86.253342 | 2016-10-04 14:16:24 |  1 | 
| 864251020174383 | 12.134140 | -86.251671 | 2016-10-04 14:19:12 |  1 | 
| 864251020174383 | 12.135820 | -86.249687 | 2016-10-04 14:19:57 |  1 | 
| 864251020174383 | 12.134770 | -86.250549 | 2016-10-04 14:19:57 |  1 | 
| 864251020174383 | 12.136580 | -86.248581 | 2016-10-04 14:20:02 |  1 | 
| 864251020174383 | 12.137000 | -86.247551 | 2016-10-04 14:20:02 |  1 | 
| 864251020174383 | 12.137160 | -86.246262 | 2016-10-04 14:20:03 |  2 | 
| 864251020174383 | 12.137080 | -86.245621 | 2016-10-04 14:22:33 |  1 | 
| 864251020174383 | 12.136490 | -86.243942 | 2016-10-04 14:23:28 |  1 | 
| 864251020174383 | 12.135990 | -86.243080 | 2016-10-04 14:43:05 |  1 | 
| 864251020174383 | 12.135820 | -86.241798 | 2016-10-04 14:43:57 |  1 | 
| 864251020174383 | 12.135820 | -86.240211 | 2016-10-04 14:47:04 |  1 | 
| 864251020174383 | 12.132720 | -86.237892 | 2016-10-04 14:49:02 |  1 | 
| 864251020174383 | 12.133971 | -86.238281 | 2016-10-04 15:11:27 |  1 | 
| 864251020174383 | 12.104250 | -86.253792 | 2016-10-11 20:01:36 |  2 | 
| 864251020174383 | 12.105340 | -86.251129 | 2016-10-11 20:01:45 |  1 | 
| 864251020174383 | 12.106010 | -86.249069 | 2016-10-11 20:02:02 |  1 | 
| 864251020174383 | 12.102820 | -86.245644 | 2016-10-11 20:02:20 |  1 | 
| 864251020174383 | 12.087050 | -86.231468 | 2016-10-11 20:02:32 |  1 | 
| 864251020174383 | 12.065980 | -86.212334 | 2016-10-11 20:02:48 |  1 | 
| 864251020174383 | 12.065560 | -86.208298 | 2016-10-11 20:02:56 |  1 | 
| 864251020174383 | 12.064720 | -86.205040 | 2016-10-11 20:03:12 |  1 | 
| 864251020174383 | 12.064050 | -86.202888 | 2016-10-11 20:03:20 |  1 | 
+-----------------+-----------+------------+---------------------+--------+ 

これは、デバイス864251020174383とデバイス864251020174384のための1つの旅行のための3つのルートである必要がありますが、イムは、それのホールドを取得していません。どんな助けもありがとう。

この

は私が持っているもので、これまで

<?php 

require_once 'includes/PolylineEncoder.php'; 
require_once 'includes/config.php'; 

$dbHost = DB_HOST; 
$dbUser = DB_USER; 
$dbPass = DB_PASS; 
$dbBase = DB_DATA; 
$charset = 'utf8'; 

$dsn = "mysql:host=$dbHost;dbname=$dbBase;charset=$charset"; 
$opt = [ 
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, 
    PDO::ATTR_EMULATE_PREPARES => false, 
]; 

$db = new PDO($dsn, $dbUser, $dbPass, $opt); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

//date_default_timezone_set('America/Miami'); 
header('Content-type: application/json'); 

if ($_POST['idUser']) { 
    $response = getGpsData($db, $_POST['idUser']); 
    echo json_encode(['gpsData' => $response]); 
} else { 
    echo json_encode(['error' => 'Faltan parametros']); 
} 

/** 
* Get ID idUser base on the name 
* @param $idUser 
*/ 
function getGpsData($db, $idUser) { 
    try { 
     // Get Devices from the user 
     $sql = "SELECT * FROM device WHERE idUser = :idUser;"; 
     $result = $db->prepare($sql); 
     $result->bindParam(':idUser', $idUser, PDO::PARAM_STR); 
     $result->execute(); 

     $devices = array(); 

     if ($result->rowCount() > 0) { 
      $result->bindColumn('idDevice', $idDevice); 
      $result->bindColumn('idVehicle', $idVehicle); 
      $result->bindColumn('idSimcard', $idSimcard); 
      $result->bindColumn('alias', $alias); 
      $result->bindColumn('imei', $imei); 
      $result->bindColumn('status', $status); 

      while ($result->fetch(PDO::FETCH_BOUND)) { 

     $routes = getGps($db, $imei); 

     $row = array('idDevice' => $idDevice, 
      'idVehicle' => $idVehicle, 
      'idSimcard' => $idSimcard, 
      'alias' => $alias, 
      'imei' => $imei, 
      'routes' => $routes); 
       array_push($devices, $row); 
      } 
      return $devices; 
     } 
    } catch (exception $e) { 
     return $e; 
    } 
} 

/** 
* Get ID idUser base on the name 
* @param $idUser 
*/ 
function getGps($db, $imei) { 

    $polylineEncoder = new PolylineEncoder(); 

    try { 
     $sql = "SELECT * FROM dataGps WHERE imei = :imei ORDER BY date;"; 
     $result = $db->prepare($sql); 
     $result->bindParam(':imei', $imei, PDO::PARAM_STR); 
     $result->execute(); 

     $routes = array(); 
     $startLocation = array(); 
     $endLocation = array(); 

     if ($result->rowCount() > 0) { 
      $i = 1; 
      $result->bindColumn('lat', $lat); 
      $result->bindColumn('lon', $lon); 
      $result->bindColumn('date', $date); 
      $result->bindColumn('engine', $engine); 
      $result->bindColumn('gpsAccuracy', $accuracy); 

      while ($result->fetch(PDO::FETCH_BOUND)) { 

       $polylineEncoder->addPoint($lat, $lon); 
       //echo "engine:" . $engine ."\n"; 

       if ($engine == 1) { 
        // Start Location 
        if ($i == 1) { 
         // Add Start Location 
         $startLocation = [ 
          'lat' => $lat, 
          'lon' => $lon, 
          'accuracy' => $accuracy 
         ]; 
         $points['startLocation'] = $startLocation; 
        } 
       } else if ($engine == 2) { 
        // Add End Location 
        $endLocation = [ 
         'lat' => $lat, 
         'lon' => $lon, 
         'accuracy' => $accuracy 
        ]; 
        $points['endLocation'] = $endLocation; 
        $points['path'] = $polylineEncoder->encodedString(); 
        $i = 1; 
       } 

       $routes[] = $points; 
       $i++; 
      } 
      return $routes; 
     } 
    } catch (exception $e) { 
     return $e; 
    } 
} 

//$decodedPoints = PolylineEncoder::decodeValue("_`dyD~ps|U_eg}@nnqC_mqNvxq`@"); 
//var_dump($decodedPoints); 
?> 

は正しい形式はこのようになります。

"routes": [ 
      { 
       "startLocation": { 
        "lat": "xxxxxx", 
        "lon": "xxxxxx", 
        "accuracy": "xx" 
       }, 
       "endLocation": { 
        "lat": "xxxxxxx", 
        "lon": "xxxxxxx", 
        "accuracy": "xx" 
       }, 
       "path": "sz_jAd`jmO" 
      }, 
      { 
       "startLocation": { 
        "lat": "xxxxxx", 
        "lon": "xxxxxx", 
        "accuracy": "xx" 
       }, 
       "endLocation": { 
        "lat": "xxxxxxx", 
        "lon": "xxxxxxx", 
        "accuracy": "xx" 
       }, 
       "path": "sz_jAd`jmO" 
      } 
    ] 

実際のJSON応答:あなたはあなたの結果にstartLocationを持っていることはありません

{ 
    "gpsData": [ 
     { 
      "idDevice": "1", 
      "idVehicle": "1", 
      "idSimcard": "1", 
      "alias": "RonEskinder", 
      "imei": "864251020174383", 
      "routes": [ 
       { 
        "endLocation": { 
         "lat": "12.137", 
         "lon": "-86.254501", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.137", 
         "lon": "-86.254501", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.137", 
         "lon": "-86.254501", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.137", 
         "lon": "-86.254501", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.137", 
         "lon": "-86.254501", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.137", 
         "lon": "-86.254501", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.137", 
         "lon": "-86.254501", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.13716", 
         "lon": "-86.246262", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoM" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.13716", 
         "lon": "-86.246262", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoM" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.13716", 
         "lon": "-86.246262", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoM" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.13716", 
         "lon": "-86.246262", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoM" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.13716", 
         "lon": "-86.246262", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoM" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.13716", 
         "lon": "-86.246262", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoM" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.13716", 
         "lon": "-86.246262", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoM" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.13716", 
         "lon": "-86.246262", 
         "accuracy": "1" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoM" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.10425", 
         "lon": "-86.253792", 
         "accuracy": "0" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoMN_CtBoIbBkD`@_G?}HjRoMyFlAvxD|_B" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.10425", 
         "lon": "-86.253792", 
         "accuracy": "0" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoMN_CtBoIbBkD`@_G?}HjRoMyFlAvxD|_B" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.10425", 
         "lon": "-86.253792", 
         "accuracy": "0" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoMN_CtBoIbBkD`@_G?}HjRoMyFlAvxD|_B" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.10425", 
         "lon": "-86.253792", 
         "accuracy": "0" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoMN_CtBoIbBkD`@_G?}HjRoMyFlAvxD|_B" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.10425", 
         "lon": "-86.253792", 
         "accuracy": "0" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoMN_CtBoIbBkD`@_G?}HjRoMyFlAvxD|_B" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.10425", 
         "lon": "-86.253792", 
         "accuracy": "0" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoMN_CtBoIbBkD`@_G?}HjRoMyFlAvxD|_B" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.10425", 
         "lon": "-86.253792", 
         "accuracy": "0" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoMN_CtBoIbBkD`@_G?}HjRoMyFlAvxD|_B" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.10425", 
         "lon": "-86.253792", 
         "accuracy": "0" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoMN_CtBoIbBkD`@_G?}HjRoMyFlAvxD|_B" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.10425", 
         "lon": "-86.253792", 
         "accuracy": "0" 
        }, 
        "path": "goaiArqmmO~HgFzFmI}B_FqEkDkFkLrAlEsBoMN_CtBoIbBkD`@_G?}HjRoMyFlAvxD|_B" 
       } 
      ] 
     }, 
     { 
      "idDevice": "2", 
      "idVehicle": "2", 
      "idSimcard": "2", 
      "alias": "Karen Molina", 
      "imei": "864251020174384", 
      "routes": [ 
       { 
        "endLocation": { 
         "lat": "12.292415", 
         "lon": "-86.236351", 
         "accuracy": "1" 
        }, 
        "path": "sz_jAd`jmO" 
       }, 
       { 
        "endLocation": { 
         "lat": "12.292415", 
         "lon": "-86.236351", 
         "accuracy": "1" 
        }, 
        "path": "sz_jAd`jmO" 
       } 
      ] 
     } 
    ] 
} 
+0

は、列の名前、日付に注意してください:

1.あなたは$エンジン= 1つのブロックにループの最後から$ Iを移動する必要があります$ iの値をとることはありませんThatsなぜ= MySQLのキーワードを使用しているため、正しくラップしたり、SQLクエリでエスケープしたりしないと、エラーが発生する可能性があります。同じことが 'エンジン'にも当てはまります。これらの事柄に別の言葉遣いを使用すると、長期的にはより簡単になります。 – Martin

+0

コメントのおかげで、私は私の名前の書式にいくつかの変更を適用します。 – RonEskinder

答えて

2

$ iは決して= 1ではないからです。データの最初の行はtime = 2016-10-04 10:53:34です。 engine = 2、$ i = 1、$ i ++を設定したので、endPointを追加します。 date`は `として

while ($result->fetch(PDO::FETCH_BOUND)) { 

      $polylineEncoder->addPoint($lat, $lon); 
      //echo "engine:" . $engine ."\n"; 

      if ($engine == 1) { 
       // Start Location 
       if ($i == 1) { 
        // Add Start Location 
        $startLocation = [ 
         'lat' => $lat, 
         'lon' => $lon, 
         'accuracy' => $accuracy 
        ]; 
        $i++; 
        $points['startLocation'] = $startLocation; 
       } 
      } else if ($engine == 2) { 
       // Add End Location 
       $endLocation = [ 
        'lat' => $lat, 
        'lon' => $lon, 
        'accuracy' => $accuracy 
       ]; 
       $points['endLocation'] = $endLocation; 
       $points['path'] = $polylineEncoder->encodedString(); 
       $i = 1; 
      } 

      $routes[] = $points; 

     }