php while while循环

时间:2016-10-13 02:26:19

标签: php json while-loop

我有以下GPS数据,即时尝试根据引擎状态获取用户的行程(路线),并按engine=1为引擎启动的日期排序,engine=2为发动机关闭事件,所以每当我找到engine=2我知道这是一次旅行的结束。我需要以JSON格式呈现这次旅行,即时通过php进行此操作,但我仍然坚持单独进行实际旅行。

任何帮助将不胜感激。

    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应该是3路由,对于设备864251020174384应该是一次路由,但我没有得到它的保留。任何帮助将不胜感激。

这是我到目前为止所拥有的

<?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响应:

    {
    "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"
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:2)

你的结果数组中永远不会有startLocation,因为$ i永远不会= 1.数据的第一行有时间= 2016-10-04 10:53:34。它添加了一个endPoint,因为引擎= 2,设置$ i = 1并且在设置$ i ++之后。这就是为什么$ i永远不会取值= 1。

你需要将$ i从循环结束移动到$ engine = 1 block:

 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;

        }