Filter in a Select MYSQL via PHP (Ionic 3)

Asked

Viewed 198 times

1

Good afternoon to everyone, before explaining a little more about the doubt I would like to make it clear that I am very lay in the subject and, amazingly, I’ve been trying to solve this problem for weeks. Doubt: I would like to list some data from my BD, in case the user would enter a date and the system would list only records containing that date, as if it were a filter. After several tests I only managed to make a simple list, without any filter, follows the codes:(home.ts)

    carregarProdutos(){

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

  });

}

products.ts(Preview)

  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, but I can not in any way make the variable transport so that it stays like this:

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

Again I’m sorry, but I’m really layabout.

  • What framework is sweating in PHP.

  • Restful API with PHP Slim Framework, that would be it?

  • Follow git: https://github.com/ClubeDosGeeksCoding/api-php-slim-framework

2 answers

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 the listing of the data on the TS itself, but anyway I leave the record to anyone who may have this doubt in the future.

0


You can pass the date as a parameter in your route

On the client side, add method with a date parameter for date search, at http request url pass date: /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 path with the date parameter, {date}

$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); 
    }
  • Thank you so much for the answer! Soon I will test, but thank you in advance.

  • A doubt, I can declare the variable like this? data= "2018-07-20"; If yes I’m having a problem, follow the link: link simply gives this message on the console and does not list anything.

  • You can test your api directly without using the front end just put the same URL in the browser and test the answer

  • I forgot that detail, a thousand apologies. link

  • Do not forget that date has to be among pelicas ?2018-02-12' for example

  • Yes, follow the home.ts: data= "2018-07-20"; load Products(){ this.productProvider.getAll(this.data) . then(data => { this.products = data; }); }

  • Isolate the problem first secure the api on the server by making the request directly in the browser. In fact you should keep the previous api and also test in the browser to check if you are getting some error, if the answer is correct advances to the front-end

  • I will break a little more the head then haha. Thank you so much for everything guy.

Show 3 more comments

Browser other questions tagged

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