<?

    $json = file_get_contents('php://input');   
    $obj = json_decode($json);
    $id = $obj->flag;
    // Returns a Json
    header('Content-Type: application/json');
    $array;
    
    $conn = new mysqli("directoriofitness.com.mx", "direct18", "L4LL4v3M@3str@","direct18_ProGymCloud");
    $conn -> set_charset("utf8");
    $query = "SELECT * FROM gimnasios WHERE NombreGimnasio = '$obj->gym';";
    
    $res = $conn->query($query);
    
    $row = $res->fetch_assoc();
    
    $idGym = $row["idGimnasio"];
    
    $conn->close();
    
    $conn = new mysqli("directoriofitness.com.mx", "direct18", "L4LL4v3M@3str@","direct18_ProGymWorkout");
    $conn->set_charset("utf8");
    $query = "SELECT
        	rutinas.nombre_rutina AS 'name_rut',
            rutinas.idserie AS 'id_serie',
            series.idejercicio AS 'id_ex',
            ejercicios.ejer_name AS 'name_ex',
            ejercicios.ejer_des AS 'desc_ex',
            ejercicios.ejer_vid AS 'video_ex',
            rutinas.rut_frec AS 'series_ex',
            series.serie_noreps AS 'reps_ex',
            series.serie_carga AS 'carga_ex',
            series.serie_tdescanso AS 'descanso_ex',
            series.serie_tcarrera AS 'tiempo_carrera_ex',
            series.serie_pulso AS 'pulso_ex',
            series.serie_distancia AS 'distancia_ex',
            ejercicios.ejer_f1 AS 'f1_ex',
            ejercicios.ejer_f2 AS 'f2_ex',
            ejercicios.ejer_f3 AS 'f3_ex',
            ejercicios.ejer_f4 AS 'f4_ex',
            rutinas.rut_indice 
        FROM rutinas
        INNER JOIN series
        	ON rutinas.idserie = series.idserie
        INNER JOIN ejercicios
        	ON ejercicios.idejercicio = series.idejercicio
        WHERE
        	rutinas.idrutina = $obj->id_r
        	AND rutinas.idGym = $idGym";
    
$resultado = $conn->query($query);
if($resultado->num_rows > 0){
   
    $array = [];
    while($row = $resultado->fetch_assoc()){
        $array[] = $row;
    }
    

    usort($array, function($a, $b) {
        return $a['rut_indice'] - $b['rut_indice'];
    });
}
echo json_encode($array);
    $conn->close();
    

?>