num_rows < 1) { echo '{"error":"No devices found"}'; exit; } $str = '{"succes":true, "devices":['; $count = 1; while($result = $query->fetch_assoc()) { $str .= '{"device":"' . $result['dev_id'] . '",'; $str .= '"name":"' . $result['name'] . '",'; $str .= '"description":"' . $result['description'] . '",'; $str .= '"location":"' . $result['location'] . '"'; if($deviceid == $result['id']) $str .= ', "selected":true'; if($count == $query->num_rows) $str .= '}'; else $str .= '},'; $count++; } $str .= ']}'; echo $str; ?>