From 25e3485ba1ed3d0d9d9cb93914c8e36d21a14e3a Mon Sep 17 00:00:00 2001 From: filesite Date: Sun, 7 Jul 2024 07:38:12 +0800 Subject: [PATCH] pagination done --- plugins/Html.php | 91 ++++++++++++++++++++++++++++++ themes/beauty/views/site/index.php | 16 +++++- 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/plugins/Html.php b/plugins/Html.php index 764d5c9..d6e36e6 100644 --- a/plugins/Html.php +++ b/plugins/Html.php @@ -142,4 +142,95 @@ eof; return "{$cdn}{$localImgUrl}"; } + //参数:page、limit + public static function getPaginationLink($url, $page, $pageSize = 24) { + $arr = explode('?', $url); + if (count($arr) == 1) { //不含问号 + return "{$url}?page={$page}&limit={$pageSize}"; + } + + $paginationParams = array('page', 'limit'); + $newParms = array(); + $baseUrl = $arr[0]; + $queryString = $arr[1]; + $params = explode('&', $queryString); + if (count($params) > 0) { + foreach ($params as $item) { + list($name, $val) = explode('=', $item); + if (!in_array($name, $paginationParams)) { + array_push($newParms, $item); + } + } + } + + return $baseUrl . "?page={$page}&limit={$pageSize}" . (!empty($newParms) ? '&'.implode('&', $newParms) : ''); + } + + //输出翻页组件,page从1开始 + public static function getPaginationHtmlCode($page, $limit, $total) { + $currentUrl = FSC::$app['requestUrl']; + $maxPage = ceil($total / $limit); + + //上一页 + $previousLink = << + 上一页 + +eof; + if ($page > 1) { + $url = self::getPaginationLink($currentUrl, $page-1, $limit); + $previousLink = << + 上一页 + +eof; + } + + //下一页 + $nextLink = << + 下一页 + +eof; + if ($page < $maxPage) { + $url = self::getPaginationLink($currentUrl, $page+1, $limit); + $nextLink = << + 下一页 + +eof; + } + + //中间显示 10 页 + $otherLinks = ''; + $startPage = $page > 5 ? $page - 5 : 1; + $endPage = $startPage + 10 < $maxPage ? $startPage + 10 : $maxPage; + for ($i = $startPage; $i <= $endPage; $i ++) { + $url = self::getPaginationLink($currentUrl, $i, $limit); + if ($i != $page) { + $otherLinks .= <<{$i} +eof; + }else { + $otherLinks .= << + {$i} + +eof; + } + } + + $html = << +
    + {$previousLink} + {$otherLinks} + {$nextLink} +
+ +eof; + + return $html; + } + } diff --git a/themes/beauty/views/site/index.php b/themes/beauty/views/site/index.php index 4341515..237c2b2 100644 --- a/themes/beauty/views/site/index.php +++ b/themes/beauty/views/site/index.php @@ -168,13 +168,20 @@ eof; if (!empty($category['files'])) { //一级目录支持 $total = count($category['files']); //翻页支持 + $pageStartIndex = ($viewData['page']-1) * $viewData['pageSize']; $index = 0; foreach ($category['files'] as $file) { if (!in_array($file['extension'], $imgExts)) { continue; } - if ($index >= $viewData['pageSize']) {break;} //翻页支持 + //翻页支持 + if ($index < $pageStartIndex) { + $index ++; + continue; + }else if ($index >= $pageStartIndex + $viewData['pageSize']) { + break; + } $title = !empty($file['title']) ? $file['title'] : $file['filename']; @@ -214,4 +221,11 @@ eof; ?> + + +
+
\ No newline at end of file