From 2b9a762a0f1d284dc0c6a1e7c26f908a954074c4 Mon Sep 17 00:00:00 2001 From: douglas Date: Fri, 7 Feb 2020 17:43:39 -0300 Subject: [PATCH] Search System --- app/Controller/UserController.php | 2 +- app/Functions/helpers.php | 4 ++++ app/traits/Read.php | 18 ++++++++++++++++++ src/Paginate.php | 3 ++- views/home.html | 3 +++ 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/Controller/UserController.php b/app/Controller/UserController.php index b36c317..d5a28b5 100755 --- a/app/Controller/UserController.php +++ b/app/Controller/UserController.php @@ -28,7 +28,7 @@ public function index(){ $this->view("home", [ - "users" => $this->users->select()->paginate(2)->get(), + "users" => $this->users->select()->busca("nome,email")->paginate(5)->get(), "title" => "Listando Usuários", "links" => $this->users->links() ] diff --git a/app/Functions/helpers.php b/app/Functions/helpers.php index e8992c9..172edb4 100644 --- a/app/Functions/helpers.php +++ b/app/Functions/helpers.php @@ -52,4 +52,8 @@ function success($message){ function back(){ Redirect::back(); +} + +function busca(){ + return filter_input(INPUT_GET,"s",FILTER_SANITIZE_STRING); } \ No newline at end of file diff --git a/app/traits/Read.php b/app/traits/Read.php index 1e77473..15a543d 100644 --- a/app/traits/Read.php +++ b/app/traits/Read.php @@ -99,6 +99,7 @@ public function paginate($perPage){ $this->paginate = new Paginate(); $this->paginate->getRegisters($this->count()); $this->paginate->paginate($perPage); + $this->sql .= $this->paginate->sqlPaginate(); return $this; @@ -107,4 +108,21 @@ public function paginate($perPage){ public function links(){ return $this->paginate->links(); } + + public function busca($fields){ + + $fields = explode(",",$fields); + + $this->sql .= " WHERE "; + + foreach($fields as $field){ + $this->sql .= " {$field} LIKE :{$field} OR "; + $this->binds[$field] = "%".busca()."%"; + } + + $this->sql = rtrim($this->sql,"OR "); + + return $this; + + } } \ No newline at end of file diff --git a/src/Paginate.php b/src/Paginate.php index 15064f5..197f013 100644 --- a/src/Paginate.php +++ b/src/Paginate.php @@ -37,7 +37,7 @@ public function getRegisters($registers){ } public function sqlPaginate(){ - return " LIMIT {$this->perPage} OFFSET {$this->offSet} "; + return " LIMIT {$this->perPage} OFFSET {$this->offSet} "; } public function paginate($perPage){ @@ -46,4 +46,5 @@ public function paginate($perPage){ $this->offSet(); $this->pages(); } + } \ No newline at end of file diff --git a/views/home.html b/views/home.html index 0b87123..d4cc851 100755 --- a/views/home.html +++ b/views/home.html @@ -5,6 +5,9 @@

Usuários

NOVO +
+ +
{{ message("message") }}