API that has objects inside the main

1

I have this API:

if(isset($_GET['id']))
    {
        $json = '';
        $banner = true;
        $id=$_GET["id"];
        $connection = new MySqlServerConnection();


        $query = 'SELECT s.id,s.description,s.ipAddress,r.id AS readingId,r.value as readingValue,r.registerDate as readingDate FROM stations as s
                            INNER JOIN readings as r on s.id = idStation
                            WHERE  s.id = ?';
        $result=$connection->executeQuery($query,array($id));

            $json .= '{"status":0, "stations":[';
            foreach ($result as $row) {
            $row = (Object)$row;
            if(!$banner) { $json .= ','; } else { $banner = false; }
            $json .= '{
                "id": "'.$row->id.'",
                "description": "'.$row->description.'",
                "ipAddress": "'.$row->ipAddress.'",
                "readingId": "'.$row->readingId.'",
                "readingValue": "'.$row->readingValue.'",
                "readingDate": "'.$row->readingDate.'"
            }';
        }

        $json .= ']}';
        echo $json;

    }

The 3 first data belongs to stations , and the following 3 to readings through the stations table I do inner join bringing the data reading depending on stations is selected .

I would like something like this:

status: 0,
stations: {
    id: X,
    description: XXX,
    ipAddress: XXX.XXX.X.X,
    readings: 
    [
      {
      "readingId":   XXX,
      "readingValue" XXX:,
      "readingDate"  XXX: 
      }
    ]
}

I have tried several ways but I do not succeed, someone who could help me with this?

Thank you.

    
asked by Pato 04.12.2018 в 01:25
source

0 answers