Created
February 13, 2015 09:30
-
-
Save forthxu/38b77849b0eab7d5cfc6 to your computer and use it in GitHub Desktop.
php独立分页类
This file contains hidden or 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 | |
| /** | |
| * @Date : 13-12-5 下午12:46 | |
| * @File : Pagination.php | |
| * @Desc : | |
| * * 独立分页类 | |
| * * 调用方式: | |
| * * $pagenation = new Pagination( 4, 10, 200 ); // 4(第一个参数) = currentPage, 10(第二个参数) = pageSize, 200(第三个参数) = 总数 | |
| * * $pagenation->set_link( 'http://www.exmple.com' ); | |
| * * echo $pagenation->create(); | |
| */ | |
| class Pagination | |
| { | |
| protected $_total = 0; | |
| protected $_total_page = 0; | |
| protected $_page = 1; | |
| protected $_page_size = 20; | |
| public $_link = ''; | |
| public $_grep = 2; | |
| public $css_next = ''; | |
| public $css_prev = ''; | |
| public $css_curr = ''; | |
| public $css_page = ''; | |
| public function __construct($page, $page_size, $total) | |
| { | |
| $this->set_current_page($page); | |
| $this->set_page_size($page_size); | |
| $this->set_total($total); | |
| } | |
| public function set_link($link) | |
| { | |
| $this->_link = $link; | |
| } | |
| public function set_page_size($page_size) | |
| { | |
| if (empty($page_size)) { | |
| $this->_page_size = 20; | |
| } else { | |
| $this->_page_size = (int)$page_size; | |
| } | |
| } | |
| public function set_total($total) | |
| { | |
| $page_size = empty($this->_page_size) ? 20 : $this->_page_size; | |
| $this->_total = $total; | |
| if (0 == ($total % $page_size)) { | |
| $this->_total_page = intval($total / $page_size); | |
| } else { | |
| $this->_total_page = intval($total / $page_size) + 1; | |
| } | |
| if ($this->_page > $this->_total_page) { | |
| $this->_page = $this->_total_page; | |
| } | |
| } | |
| public function set_current_page($page) | |
| { | |
| if (empty($page)) { | |
| $this->_page = 1; | |
| } else { | |
| $this->_page = (int)$page; | |
| } | |
| } | |
| public function get_next_page_btn() | |
| { | |
| if ($this->_page < $this->_total_page) { | |
| $link = sprintf($this->_link, ($this->_page + 1)); | |
| return sprintf($this->css_next, $link); | |
| } | |
| return ''; | |
| } | |
| public function get_prev_page_btn() | |
| { | |
| if ($this->_page > 1) { | |
| $link = sprintf($this->_link, ($this->_page - 1)); | |
| return sprintf($this->css_prev, $link); | |
| } | |
| return ''; | |
| } | |
| public function get_current_page() | |
| { | |
| return sprintf($this->css_curr, $this->_page); | |
| } | |
| public function get_page_link($page) | |
| { | |
| $link = ''; | |
| $link = sprintf($this->_link, $page); | |
| $link = sprintf($this->css_page, $link, $page); | |
| return $link; | |
| } | |
| public function get_prev_pages() | |
| { | |
| $pages = array(); | |
| $begin = $this->_page - $this->_grep; | |
| if ($begin < 1) { | |
| $begin = 1; | |
| } elseif ($begin > 2) { | |
| $pages[] = $this->get_page_link(1); | |
| } elseif ($begin == 2) { | |
| $pages[] = $this->get_page_link(1); | |
| } | |
| for ($i = $begin; $i < $this->_page; $i++) { | |
| $pages[] = $this->get_page_link($i); | |
| } | |
| return $pages; | |
| } | |
| public function get_next_pages() | |
| { | |
| $pages = array(); | |
| $begin = $this->_page + 1; | |
| if ($begin < $this->_total_page) { | |
| $end = $begin + $this->_grep; | |
| if ($end > $this->_total_page) { | |
| $end = $this->_total_page; | |
| } | |
| for ($i = $begin; $i < $end; $i++) { | |
| $pages[] = $this->get_page_link($i); | |
| } | |
| if ($i < $this->_total_page) { | |
| $pages[] = $this->get_page_link($this->_total_page); | |
| } else { | |
| $pages[] = $this->get_page_link($this->_total_page); | |
| } | |
| } elseif ($begin == $this->_total_page) { | |
| $pages[] = $this->get_page_link($this->_total_page); | |
| } | |
| return $pages; | |
| } | |
| public function create() | |
| { | |
| $str = ''; | |
| if ($this->_total_page <= 1) { | |
| return $str; | |
| } | |
| // $str .= '<div class="paging">'; | |
| $str .= $this->get_prev_page_btn(); | |
| $prev_pages = $this->get_prev_pages(); | |
| if (!empty($prev_pages)) { | |
| foreach ($prev_pages as $page) { | |
| $str .= $page; | |
| } | |
| } | |
| $str .= $this->get_current_page(); | |
| $next_pages = $this->get_next_pages(); | |
| if (!empty($next_pages)) { | |
| foreach ($next_pages as $page) { | |
| $str .= $page; | |
| } | |
| } | |
| $str .= $this->get_next_page_btn(); | |
| // $str .= "第{$this->_page}/{$this->_total_page}页,共{$this->_total}条"; | |
| // $str .= '</div>'; | |
| return $str; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment