diff --git a/app/Controller/UserController.php b/app/Controller/UserController.php index 355397f..fcb808c 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()->get(), + "users" => $this->users->select()->paginate(2)->get(), "title" => "Listando Usuários" ] ); diff --git a/app/Model/User.php b/app/Model/User.php index e0ff83b..5084dc1 100755 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -6,5 +6,5 @@ class User extends Model { - protected $table = "user"; + protected $table = "users"; } \ No newline at end of file diff --git a/app/traits/Links.php b/app/traits/Links.php new file mode 100644 index 0000000..54b38b0 --- /dev/null +++ b/app/traits/Links.php @@ -0,0 +1,24 @@ +page > 1){ + + } + } + + private function next(){ + + } + + public function links(){ + + } +} \ No newline at end of file diff --git a/app/traits/Read.php b/app/traits/Read.php index a3c064b..4a7653f 100644 --- a/app/traits/Read.php +++ b/app/traits/Read.php @@ -3,13 +3,13 @@ namespace App\traits; - - use App\Model\User; +use Core\Paginate; + trait Read { - + use Links; /** * Método responsável por * @param string $fields @@ -30,6 +30,16 @@ public function bindAndExecute():\PDOStatement{ return $select; } + /** + * Método responsável por Obter a quantidade de registros para a paginação. + * @return Int + */ + public function count():int { + $select = $this->connection->prepare($this->sql); + $select->execute($this->binds); + return $select->rowCount(); + } + /** * Método responsável por obter apenas o 1º Registro encontrado. * @return object @@ -83,4 +93,14 @@ public function where():User{ return $this; } + + public function paginate($perPage){ + + $this->paginate = new Paginate(); + $this->paginate->getRegisters($this->count()); + $this->paginate->paginate($perPage); + $this->sql .= $this->paginate->sqlPaginate(); + + return $this; + } } \ No newline at end of file diff --git a/src/Model.php b/src/Model.php index 5b29261..cf9068c 100755 --- a/src/Model.php +++ b/src/Model.php @@ -18,6 +18,7 @@ class Model extends Connection protected $value; protected $sql; protected $binds; + protected $paginate; use Create,Read,Update,Delete; diff --git a/src/Paginate.php b/src/Paginate.php new file mode 100644 index 0000000..6a3d569 --- /dev/null +++ b/src/Paginate.php @@ -0,0 +1,45 @@ +page = filter_input(INPUT_GET,"page",FILTER_SANITIZE_NUMBER_INT) ?? 1; + } + + private function perPage($perPage){ + $this->perPage = $perPage ?? 30; + } + + private function offSet(){ + $this->offSet = ($this->page * $this->perPage) - $this->perPage; + } + + private function pages(){ + $this->pages = ceil($this->registers/$this->perPage); + } + + public function getRegisters($registers){ + $this->registers = $registers; + } + + public function sqlPaginate(){ + return " LIMIT {$this->perPage} OFFSET {$this->offSet} "; + } + + public function paginate($perPage){ + $this->current(); + $this->perPage($perPage); + $this->offSet(); + $this->pages(); + } +} \ No newline at end of file