Skip to content

Commit

Permalink
支持bootstrap分页工具
Browse files Browse the repository at this point in the history
  • Loading branch information
LincolnBruce committed Apr 24, 2018
1 parent 8bb0735 commit 8ffb6d8
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 61 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
vendor
.idea
log
.idea
.DS_Store
216 changes: 167 additions & 49 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ use libs\Curl\LCurl;
require __DIR__ . '/../src/autoload.php';
$curl = new LCurl();
$result = $curl->post('http://www.baidu.com/', ['a'=>'123']);
```
```
115 changes: 109 additions & 6 deletions src/Utils/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,108 @@
*/
class Page
{
private $page = 1;
private $total = 1;
private $pageSize = 10;
private $gets = [];

/**
* Page constructor.
* @param $totalPage int 总条数
* @param int $pageSize 每页显示条数
* @param int $page 当前页码
* @param array $gets url携带get参数数组
*/
public function __construct($totalPage, $pageSize = 10, $page = 1, $gets = [])
{
if ($pageSize < 1) {
$pageSize = 1;
}

$total = ceil($totalPage / $pageSize);

if ($page < 1) {
$page = 1;
} else if ($page > $total) {
$page = $total;
}

$this->page = $page;
$this->gets = $gets;
$this->total = $total;
$this->pageSize = $pageSize;
}

/**
* 获取分页html元素
* @param $url string
* @return string
*/
public function getPagination($url)
{
$gets = null;
$gets = $this->gets;
$page = $this->page;
$gets['pageSize'] = $this->pageSize;
if ($page > 1) {
$gets['page'] = 1;
$newUrl = URLUtil::addParameter($url, $gets);
$laquo = "<li><a href=\"{$newUrl}\"></a>&laquo;</li>";
$gets['page'] = $page - 1;
$newUrl = URLUtil::addParameter($url, $gets);
$left = "<li><a href=\"{$newUrl}\"></a>&lt;</li>";
} else {
$laquo = "<li class=\"disabled\">&laquo;</li>";
$left = "<li class=\"disabled\">&lt;</li>";
}
if ($page < $this->total) {
$gets['page'] = $this->total;
$newUrl = URLUtil::addParameter($url, $gets);
$raquo = "<li><a href=\"{$newUrl}\"></a>&raquo;</li>";
$gets['page'] = $page + 1;
$newUrl = URLUtil::addParameter($url, $gets);
$right = "<li><a href=\"{$newUrl}\"></a>&gt;</li>";
} else {
$raquo = "<li class=\"disabled\">&raquo;</li>";
$right = "<li class=\"disabled\">&gt;</li>";
}

$otherPages = [];
$start = 1;
if ($this->total > 5) {
$start = $page;
while (true) {
if ($start - 2 >= 1 && $start + 2 <= $this->total) {
break;
} else if ($start - 2 <= 1) {
$start++;
} else if ($start + 2 >= $this->total) {
$start--;
}
}
$end = $start + 2;
$start -= 2;
} else {
$end = $this->total;
}
for ($i = $start; $i <= $end; $i++) {
if ($page == $i) {
$otherPages[] = "<li class=\"active\"><a href=\"#\">{$i}</span></a></li>";
} else {
$gets['page'] = $i;
$newUrl = URLUtil::addParameter($url, $gets);
$otherPages[] = "<li><a href=\"{$newUrl}\">{$i}</span></a></li>";
}
}

array_unshift($otherPages, $left);
array_unshift($otherPages, $laquo);
array_push($otherPages, $right);
array_push($otherPages, $raquo);
$html = '<ul class="pagination">' . join("\n", $otherPages) . '</ul>';
return $html;
}

/**
* 获取分页参数
*
Expand Down Expand Up @@ -38,13 +140,14 @@ public static function get($page, $totalCount, $pageSize = 30)
];
}


/**
* 当前页
*
* @param $page
* @return int
*/
public static function getPage($page)
private static function getPage($page)
{
$page = (int)$page;
return !$page ? 1 : $page;
Expand All @@ -57,7 +160,7 @@ public static function getPage($page)
* @param $pageSize
* @return int
*/
public static function getPageCount($totalCount, $pageSize)
private static function getPageCount($totalCount, $pageSize)
{
$totalCount = $totalCount < 0 ? 0 : (int)$totalCount;
return ceil($totalCount / $pageSize);
Expand All @@ -71,7 +174,7 @@ public static function getPageCount($totalCount, $pageSize)
* @param $pageCount
* @return int
*/
public static function getNext($page, $pageCount)
private static function getNext($page, $pageCount)
{
if ($pageCount > 1 && $page >= 0 && $page < $pageCount) {
return $page + 1;
Expand All @@ -86,7 +189,7 @@ public static function getNext($page, $pageCount)
* @param $page
* @return int
*/
public static function getPre($page)
private static function getPre($page)
{
if ($page <= 1) {
return 0;
Expand All @@ -102,7 +205,7 @@ public static function getPre($page)
* @param $pageSize
* @return int
*/
public static function getOffset($page, $pageSize)
private static function getOffset($page, $pageSize)
{
return $pageSize < 1 ? 0 : ($page - 1) * $pageSize;
}
Expand All @@ -113,7 +216,7 @@ public static function getOffset($page, $pageSize)
* @param $pageSize
* @return int
*/
public static function getLimit($pageSize)
private static function getLimit($pageSize)
{
return $pageSize < 1 ? -1 : $pageSize;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Utils/URLUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace libs\Utils;

use libs\Validate\LValidator;

/**
* Class URLUtil.
* URL地址处理工具类.
Expand Down Expand Up @@ -32,7 +30,7 @@ public static function toHttps($url)
*/
public static function addParameter($url, $params)
{
if (!$url || !LValidator::isUrl($url)) {
if (!$url) {
return $url;
}
if ($params) {
Expand All @@ -57,7 +55,7 @@ public static function addParameter($url, $params)
*/
public static function getParameters($url)
{
if (!$url || !LValidator::isUrl($url)) {
if (!$url) {
return false;
}
$urlQuery = [];
Expand Down
11 changes: 11 additions & 0 deletions test/TestPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Created by PhpStorm.
* User: linyang
* Date: 2018/4/24
* Time: 上午9:08
*/
require __DIR__ . '/../src/autoload.php';

$page = new \libs\Utils\Page(1, 10, 10, ['uid' => 3]);
echo $page->getPagination('/index');

0 comments on commit 8ffb6d8

Please sign in to comment.