Created
January 13, 2012 06:46
-
-
Save inakagawa/1604928 to your computer and use it in GitHub Desktop.
Data::Page on PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Pager Data Container | |
class DataPage{ | |
private $total_entries; | |
private $entries_per_page; | |
private $current_page; | |
function __construct($total_entries,$entries_per_page,$current_page){ | |
$this->total_entries($total_entries ?: 0); | |
$this->entries_per_page($entries_per_page ?: 10); | |
$this->current_page($current_page ?: 1); | |
} | |
// accessor | |
public function total_entries(){ | |
if (func_num_args() > 0){ | |
$x = func_get_arg(0); | |
$this->total_entries = $x; | |
return $x; | |
} | |
return $this->total_entries; | |
} | |
public function entries_per_page(){ | |
if (func_num_args() > 0){ | |
$x = func_get_arg(0); | |
if ($x < 0){die('entries_per_page must be positive value');} | |
$this->entries_per_page = $x; | |
return $x; | |
} | |
return $this->entries_per_page; | |
} | |
public function current_page(){ | |
if (func_num_args() > 0){ | |
$x = func_get_arg(0); | |
$this->current_page = $x; | |
return $x; | |
} | |
return $this->current_page; | |
} | |
// ==== first_page/last_page | |
public function first_page(){ | |
return 1; | |
} | |
public function last_page(){ | |
$pages = $this->total_entries() / $this->entries_per_page(); | |
$last_page = 0; | |
// ページあたり項目数で割り切れるならそのまま | |
$intpages = (int)floor($pages); | |
if ($pages === $intpages){ | |
$last_page = $pages; | |
}else{ | |
$last_page = 1 + $intpages; | |
} | |
if ($last_page < 1){ $last_page = 1;} | |
return $last_page; | |
} | |
// ==== first/last : current pageでの最初最後 | |
public function first(){ | |
if ($this->total_entries() == 0){ | |
return 0; | |
}else{ | |
return ((($this->current_page() - 1) * $this->entries_per_page() ) + 1); | |
} | |
} | |
public function last(){ | |
if ($this->current_page() == $this->last_page()){ | |
return $this->total_entries(); | |
}else{ | |
return ($this->current_page() * $this->entries_per_page()); | |
} | |
} | |
public function entries_on_this_page(){ | |
if ($this->total_entries() == 0){ | |
return 0; | |
}else{ | |
return $this->last() - $this->first() + 1; | |
} | |
} | |
public function previous_page(){ | |
$cpage = $this->current_page(); | |
if ($cpage > 1){ | |
return $cpage - 1; | |
}else{ | |
return null; | |
} | |
} | |
public function next_page(){ | |
$cpage = $this->current_page(); | |
if ($cpage < $this->last_page()){ | |
return $cpage + 1; | |
}else{ | |
return null; | |
} | |
} | |
// splice: retrieve 'current' items from the given array | |
public function splice(){} | |
// skipped: | |
public function skipped(){} | |
public function change_entries_per_page(){} | |
// get navigation | |
// pagenums=(1..10), currentpage=2, | |
public function pages_in_navigation($ppn = null){ | |
$pages_per_navigation = $ppn ?: 10; | |
$first_page = $this->first_page(); | |
$last_page = $this->last_page(); | |
$current_page = $this->current_page(); | |
// if the range of navi is all included | |
if ($pages_per_navigation >= $last_page){ | |
return range($first_page, $last_page); | |
} | |
// otherwise (ex. navi has 5 slots, records require 10 pages) | |
$prev = $current_page - 1; | |
$next = $current_page + 1; | |
$cnt = 0; | |
$putcnt = 0; | |
$retarr = array($current_page); | |
while($putcnt < $pages_per_navigation - 1){ | |
if ($cnt % 2){ //prev | |
if ($first_page <= $prev){ | |
array_unshift($retarr, $prev); | |
$putcnt += 1; | |
} | |
$prev -= 1; | |
}else{ //next | |
if ($last_page >= $next){ | |
array_push($retarr, $next); | |
$putcnt += 1; | |
} | |
$next += 1; | |
} | |
$cnt++; | |
} | |
return $retarr; | |
} | |
public function first_navigation_page(){ | |
$arr = $this->pages_in_navigation(); | |
return $arr[0]; | |
} | |
public function last_navigation_page(){ | |
$arr = $this->pages_in_navigation(); | |
return $arr[-1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment