Filter in a Select MYSQL via PHP (ionic 3)

1

Good afternoon everyone, before explaining a little more about the doubt I would like to make it clear that I am very lazy on the subject and, incredible as it may seem, I have been trying for weeks to solve this problem. Doubt: I would like to list some data from my DB in case the user would enter a date and the system would only list records containing that date as if it were a filter. After several tests I was able to do a simple list without any filter, follow the codes: (home.ts)

    carregarProdutos(){

  this.produtoProvider.getAll()
  .then(data => {
    this.produtos = data;

  });

}

productos.ts (provider)

  getAll() {
return new Promise(resolve => {
  this.http.get(this.URL+'/produto').subscribe(data => {
    resolve(data);
  }, err => {
    console.log(err);
  });
});
}

Index.php

$app->get('/produto/', function() use ($app){
   (new \controllers\Produto($app))->lista();
});

Controller product.php

        public function lista(){
            global $app;
            $sth = $this->PDO->prepare("SELECT * FROM produtos");
            $sth->execute();
            $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
            $app->render('default.php',["data"=>$result],200); 
        }

As I said this code is working, however I can not do the variable transport in any way so it looks like this:

$sth = $this->PDO->prepare("SELECT * FROM produtos WHERE data= :data");
$sth ->bindValue(':data',$data);

Again I'm sorry, but I'm really lazy on the subject.

    
asked by anonymous 20.07.2018 / 16:53

2 answers

0

You can pass the date as a parameter on your route

On the client side, add method with a date parameter to search by date, in the url of the http request passes data: / product / '+ date

getAllByDate(data) {
return new Promise(resolve => {
  this.http.get(this.URL+'/produto/'+ data).subscribe(data => {
    resolve(data);
  }, err => {
    console.log(err);
  });
});
}

On the server side, add a route with the date parameter,

$app->get('/produto/{data}', function($request, $response, $args) use ($app){

   //o valor da data é passado no array $args
   (new \controllers\Produto($app))->lista($args['data']);
});

    //data é passada como argumento para o seu método
    public function lista($data){
        global $app;
        $sth = $this->PDO->prepare("SELECT * FROM produtos WHERE               data= :data");

       $sth ->bindValue(':data',$data);
        $sth->execute();
        $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
        $app->render('default.php',["data"=>$result],200); 
    }
    
20.07.2018 / 19:38
0

First of all I would like to thank Jorge, he helped me a lot in the resolution. I will explain here how the functional code was. index.php

$app->get('/produto/:datad', function($datad) use ($app){
(new \controllers\Produto($app))->lista($datad);

}); Controller:

public function lista($datad){
    global $app;
    $sth = $this->PDO->prepare("SELECT * FROM consumo_atual where data_criacao = :data");
    $sth ->bindValue(':data',$datad);
    $sth->execute();
    $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
    $app->render('default.php',["data"=>$result],200); 
}

Now my only problem is in listing the data in TS itself, but anyway I leave the record to everyone who might have this doubt in the future.

    
21.07.2018 / 01:07