Error listing MYSQL data via RESTful API with PHP (Ionic 3)

1

Good morning, I have a problem in listing data in ionic 3 using mysql. The problem is the following, I have an app that lists the comodo of a house and the consumption of each comodo, but I can not get the specific id of each room so that I can list its consumption.

It was to list this way:

FollowtheCodes(home.ts):

publiccarregarComodos(){this.Id_usuario={id:this.id};this.consumoProvider.getComodos(this.Id_usuario).then(data=>{this.comodo=data;console.log(this.comodo.id)this.Id_usuario2={id:this.comodo.id};this.consumoProvider.getAll(this.Id_usuario2).then(data=>{this.consumo=data;});});

Provider

getAll(id){returnnewPromise(resolve=>{letheaders=newHeaders();headers.append('Content-Type','application/json');letoption=newRequestOptions({headers:headers});this.http.post(this.URL+'/usuarios/select_usuariozinho',JSON.stringify(id)).subscribe(data=>{resolve(data);},err=>{console.log(err);});});getComodos(id){returnnewPromise(resolve=>{letheaders=newHeaders();headers.append('Content-Type','application/json');letoption=newRequestOptions({headers:headers});this.http.post(this.URL+'/usuarios/select',JSON.stringify(id)).subscribe(data=>{resolve(data);},err=>{console.log(err);});});

index.php

$app->post('/usuarios/select_usuariozinho',function()use($app){(new\controllers\produto($app))->select_usuariozinho();});$app->post('/usuarios/select',function()use($app){(new\controllers\produto($app))->select();});

Controller

publicfunctionselect_usuariozinho(){global$app;$dados=json_decode($app->request->getBody(),true);$dados=(sizeof($dados)==0)?$_POST:$dados;$id=$dados["id"];
        $sth = $this->PDO->prepare("SELECT * FROM consumo_atual WHERE 
        comodo_id = :id order by id desc limit 1");
        $sth ->bindValue(':id',$id);
        $sth->execute();
        $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
        $app->render('default.php',["data"=>$result],200); 
    }
public function select(){
        global $app; 
        $dados = json_decode($app->request->getBody(), true);
        $dados = (sizeof($dados)==0)? $_POST : $dados;
        $id = $dados["id"];
        $sth = $this->PDO->prepare("SELECT c.id,c.descricao FROM comodo c  
        WHERE Usuario_id = :id");
        $sth ->bindValue(':id',$id);
        $sth->execute();
        $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
        $app->render('default.php',["data"=>$result],200); 
    }
    
asked by anonymous 22.07.2018 / 17:00

1 answer

0

You can do a JOIN and return the consumption already in the query of the rooms.   $sth = $this->PDO->prepare("SELECT * from Comodos como INNER JOIN consumo_atual cons ON cons.comodo_id = como.id WHERE como.Usuario_id = :id")

At this point you will receive the information of both consumption and the room, then just iterate and mount the view in the APP.

    
23.07.2018 / 13:13