/engine/libraries/pagination.php

https://github.com/wamplo/red · PHP · 113 lines · 63 code · 23 blank · 27 comment · 12 complexity · 16082a8136fb25b77a6c256114177bd5 MD5 · raw file

  1. <?php
  2. namespace Engine\libraries;
  3. if (! defined ( 'SECURE' ))
  4. exit ( 'Hello, security@networks.co.id' );
  5. /**
  6. * DONOTEDIT, extend olny at /application/models
  7. * @version 100.20/3/2011
  8. * @package ENGINE/CORE
  9. * @author rama@networks.co.id
  10. * @tutorial wiki/missing.txt
  11. * @todo cacheing should be in models ?
  12. */
  13. class Pagination {
  14. public $curroffset; # current offset
  15. public $maxperpage; #! max per page
  16. public $totalrow; #! count total row
  17. public $currrow; #! count current row
  18. public function __construct(){
  19. $this->__getPage();
  20. }
  21. public function createHtml($options = array()){
  22. $params = '';
  23. foreach ( $options as $key => $value ) {
  24. $params .= " $key = '$value'";
  25. }
  26. #var_dump($this->currrow,$this->totalrow,$this->maxperpage,$this->curroffset);
  27. # BACK
  28. if ($this->curroffset > 0) {
  29. $dataURL = parse_url($_SERVER['REQUEST_URI']); // $x = $dataURL['path'];
  30. $buildQuery = http_build_query( Array( 'offset' => $this->curroffset - 1 ) + $_GET );
  31. echo '<a id="arrow-link" href="'. $dataURL['path'] .'?'. $buildQuery .'" '.$params.'><div id="pagination-to-back"><span id="pagination-to-next-arrow"><</span></div></a>';
  32. }
  33. # NEXT
  34. # @todo masih ada bug, kalo pas post pas di nextnya gak ada.
  35. if ($this->currrow == $this->maxperpage) {
  36. #var_dump($this);
  37. $dataURL = parse_url($_SERVER['REQUEST_URI']); // $x = $dataURL['path'];
  38. $buildQuery = http_build_query( Array( 'offset' => $this->curroffset + 1 ) + $_GET );
  39. echo '<a id="arrow-link" href="'. $dataURL['path'] .'?'. $buildQuery .'" '.$params.'><div id="pagination-to-next"><span id="pagination-to-next-arrow">></span></div></a>';
  40. }
  41. }
  42. public function creatHtmlInfo(){
  43. $pages = ceil($this->totalrow/$this->maxperpage);
  44. $currpage = $this->curroffset + 1;
  45. if ($pages > 0) {
  46. echo '<div style="position: relative; top: 20px; text-align: center; padding: 2px; background: none repeat scroll 0pt 0pt rgb(232, 232, 232); color: rgb(181, 181, 181);">'.$currpage.'/'.$pages.'</div>';
  47. }
  48. }
  49. /**
  50. * createLinks
  51. *
  52. * @return HTML
  53. * @author Adam Ramadhan
  54. **/
  55. public function createHtml2(){
  56. $current = ( $this->curpage + 1 ) * $this->perpage;
  57. if ($current == 0) {
  58. $current = $this->perpage;
  59. }
  60. if ($current < $this->total ) {
  61. # @todo EFFICIENT FIX, we can use explode from ?, etc rather then parse url
  62. $this->page = $this->page + 1;
  63. $dataURL = parse_url($_SERVER['REQUEST_URI']); // $x = $dataURL['path'];
  64. $buildQuery = http_build_query( Array( 'offset' => $this->page ) + $_GET );
  65. echo '<a href="'. $dataURL['path'] .'?'. $buildQuery .'">Next</a>';
  66. }
  67. if ($current > $this->total ) {
  68. # @todo EFFICIENT FIX, we can use explode from ?, etc rather then parse url
  69. $this->page = $this->page - 1;
  70. $dataURL = parse_url($_SERVER['REQUEST_URI']); // $x = $dataURL['path'];
  71. $buildQuery = http_build_query( Array( 'offset' => $this->page ) + $_GET );
  72. echo '<a href="'. $dataURL['path'] .'?'. $buildQuery .'">Back</a>';
  73. }
  74. }
  75. /**
  76. * getPage()
  77. *
  78. * @return current page
  79. * @author Adam Ramadhan
  80. **/
  81. private function __getPage(){
  82. if (!isset($_GET['offset'])) {
  83. $this->curroffset = 0;
  84. }
  85. if (isset($_GET['offset']) && is_numeric($_GET['offset'])) {
  86. $this->curroffset = $_GET['offset'];
  87. }
  88. }
  89. }
  90. ?>