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

Asked

Viewed 141 times

1

Good morning, I have a problem listing data in Ionic 3 using mysql. The problem is this, I have an app that lists the comforts of a house and the consumption of each room, but I can’t find the specific id of each room so I can list your consumption.

It was supposed to list like this:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

Follow the Codes (home.ts):

public carregarComodos(){
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){
return new Promise(resolve => {
  let headers = new Headers();
  headers.append('Content-Type', 'application/json');
  let option= new RequestOptions({headers:headers});
  this.http.post(this.URL+'/usuarios/select_usuariozinho', JSON.stringify(id))
  .subscribe(data=>{
    resolve(data);
  }, err => {
    console.log(err);
  });
});

getComodos(id){
return new Promise(resolve => {
  let headers = new Headers();
  headers.append('Content-Type', 'application/json');
  let option= new RequestOptions({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

public function select_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); 
    }

1 answer

1


You can make a JOIN and return the consumption already in the rooms query. $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 time you will receive both the information of consumption and the room, then just iterate and assemble the view in the APP.

  • 1

    thank you very much, really worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.