Skip to content

Commit

Permalink
Search System
Browse files Browse the repository at this point in the history
  • Loading branch information
douglas committed Feb 7, 2020
1 parent 5674fa2 commit 2b9a762
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
]
Expand Down
4 changes: 4 additions & 0 deletions app/Functions/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ function success($message){

function back(){
Redirect::back();
}

function busca(){
return filter_input(INPUT_GET,"s",FILTER_SANITIZE_STRING);
}
18 changes: 18 additions & 0 deletions app/traits/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

}
}
3 changes: 2 additions & 1 deletion src/Paginate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -46,4 +46,5 @@ public function paginate($perPage){
$this->offSet();
$this->pages();
}

}
3 changes: 3 additions & 0 deletions views/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<div class="col">
<h1>Usuários</h1>
<a class="btn btn-primary my-3" href="/users/create" >NOVO</a>
<form method="GET" class="d-flex justify-content-end">
<input class="form-control col-3" type="text" name="s" placeholder="Pesquisar" aria-label="Search">
</form>
{{ message("message") }}
<table class="table table-striped">
<thead class="thead-dark">
Expand Down

0 comments on commit 2b9a762

Please sign in to comment.