Skip to content

Instantly share code, notes, and snippets.

@kanghyojun
Created April 29, 2013 10:17
Show Gist options
  • Save kanghyojun/5480788 to your computer and use it in GitHub Desktop.
Save kanghyojun/5480788 to your computer and use it in GitHub Desktop.
Pager class for board.
<?php
/** Pager class for php
*
* {{{
*
* $pager = new Page($all_data_length, $page_per_post_length);
* $page_html = "<ul>"
* foreach($pager.getPage($current_page) as $page) {
* $page_html = $page_html . "<li class=\"" . $page['flag'] . "\"></li>" . $page['num'] . "</li>"
* }
*
* $sql = "SELECT * FROM some_post OFFSET " . $pager.getOffset() . " LIMIT " . $pager.getLimit()
*
* }}}
*
* NUMBER decide how many page surround current.
*
* {{{
*
* // if NUMBER is 3
*
* $pager.getPage(40) // will contain, 1 ... 37 38 39 40 41 42 43 ... 70
* }}}
*
*/
class Page {
private $NUMBER = 3;
private $page = array();
private $offset = 0;
private $page_per_post = 10;
private $curr = 0;
/** Constructor for Page
*
* @param $data_length length of a data.
* @param $page_per_post length of a post which show in one page.
*
*/
function __construct($data_length, $page_per_post) {
$this->page_per_post = $page_per_post;
$this->last = $data_length / $page_per_post;
}
/** Push a array to $this->page with given range.
*
* @param range range that want to push $this->page
*
*/
private function pushPage($range) {
foreach($range as $n) {
if($n == $this->curr) {
array_push($this->page, array("flag" => "current", "num" => $n));
} else {
array_push($this->page, array("flag" => "page", "num" => $n));
}
}
}
/** Get page
*
* @param curr current page number
* @return page object
*
*/
public function getPage($curr) {
if($this->curr != $curr) {
if($curr > $this->last or $curr < 1) throw new Exception('Invalid Page number');
$this->curr = $curr;
$this->offset = $this->page_per_post * ($this->curr - 1);
array_push($this->page, array("flag" => "first", "num" => 1));
if($this->curr < $this->NUMBER + 2) {
$this->pushPage(range(2, $this->curr + $this->NUMBER, 1));
} else if($this->curr < $this->last - ($this->NUMBER + 2)) {
$this->pushPage(range($this->curr - $this->NUMBER, $this->curr + $this->NUMBER, 1));
} else {
$this->pushPage(range($this->curr - $this->NUMBER, $this->last - 1, 1));
}
array_push($this->page, array("flag" => "last", "num" => $this->last));
}
return $this->page;
}
/** Get offset for RDB
*
* @return offset
*
*/
public function getOffset() {
return $this->offset;
}
/** Get limit for RDB
*
* @return limit
*
*/
public function getLimit() {
return $this->page_per_post;
}
/** Set NUMBER
*
* @param num
*
*/
public function setNumber($num) {
$this->NUMBER = $num;
}
};
$pager = new Page(700, 10);
echo "---------" ;
echo var_dump($pager->getPage(3));
echo "---------" ;
echo var_dump($pager->getPage(20));
echo "---------" ;
echo var_dump($pager->getPage(67));
echo "---------\n" ;
echo "Offset : " . $pager->getOffset() . "\n";
echo "Limit : " . $pager->getLimit() . "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment