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:
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);
}
thank you very much, really worked
– André Paiva