我有这个功能,我有分页,它设置为只在链接中显示10页。所以说有11页,然后链接看起来像
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 下一步» | 最后»
然后在11页面上看起来像
«首先 | «上一页 | 11
但是,在第十页上,这就是它的样子
«首先 | «上一页 | 11 | 下一步» | 最后»
这是我正在使用的PHP函数(抱歉,它相当大)
function get_paging_link($sql, $itemPerPage = 10, $strGet = '') {
global $db2;
global $q;
$result = $db2->GetDataTable($sql);
$pagingLink = '';
$totalResults = count($result);
$totalPages = ceil($totalResults / $itemPerPage);
$numLinks = 10;
if ($totalPages > 1) {
$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
if (isset($_POST['page']) && $_POST['page'] > 0) {
$pageNumber = $_POST['page'];
} else {
$pageNumber = 1;
}
if ($pageNumber > 1) {
$page = $pageNumber - 1;
if ($page > 1) {
$prev = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',$page);return false;\">« Prev <sp>|</sp></a> ";
} else {
$prev = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',$page);return false;\">« Prev <sp>|</sp></a> ";
}
$first = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',1);return false;\">« First <sp>|</sp></a> ";
} else {
$prev = '';
$first = '';
}
if ($pageNumber < $totalPages) {
$page = $pageNumber + 1;
$next = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',$page);return false;\"><sp>|</sp> Next »</a> ";
$last = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',$totalPages);return false;\"><sp>|</sp> Last »</a> ";
} else {
$next = '';
$last = '';
}
$start = $pageNumber - ($pageNumber % $numLinks) + 1;
$end = $start + $numLinks - 1;
$end = min($totalPages, $end);
$pagingLink = array();
for ($page = $start; $page <= $end; $page++) {
if ($page == $pageNumber) {
$pagingLink[] = " $page ";
} else {
if ($page == 1) {
$pagingLink[] = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',1);return false;\">$page</a> ";
} else {
$pagingLink[] = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',$page);return false;\">$page</a> ";
}
}
}
$pagingLink = implode(' <sp>|</sp> ', $pagingLink);
$pagingLink = $first . $prev . $pagingLink . $next . $last;
}
return $pagingLink;
}
我无法弄清楚我在数学方面做错了什么。我已经玩过1和1的加减,但无济于事。提前致谢!抱歉高高的帖子
编辑:为了让问题更加清晰,当我每隔10页(10,20,30,...)时,就会显示下一组10个链接。它会在第10页显示一个页面。
编辑2:我用以下内容替换了包含分页功能的块(以$start = $pageNumber - ($pageNumber % $numLinks) + 1;
开头,在$pagingLink = implode(' <sp>|</sp> ', $pagingLink);
之前结束):
$max_page_links = 10;
if ($pageNumber > $max_page_links / 2) {
if (ceil($totalResults / $itemPerPage) - $pageNumber < (($max_page_links / 2) + 1)) {
$start_counter = $pageNumber - ($max_page_links - (ceil($totalResults / $itemPerPage) - $pageNumber)) + 1;
$end_counter = ceil($totalResults / $itemPerPage);
} else {
$start_counter = $pageNumber - (($max_page_links / 2) - 1);
$end_counter = $pageNumber + ($max_page_links / 2);
}
} else {
$start_counter = 1;
$end_counter = $max_page_links;
}
for ($i = $start_counter; $i <= $end_counter; $i++) {
if ($i == $pageNumber) {
$pagingLink[] = " $i ";
} else {
$pagingLink[] = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',$i);return false;\">$i</a> ";
}
}
答案 0 :(得分:2)
你是对的。你的等式:
$pageNumber - ($pageNumber % $numLinks) + 1 // This equates to 11 when pageNumber is 10
我使用以下内容进行分页:
// set the limit of pictures, page number, and number of pages
// multiples of 4 unless you change column count below
$limit = $itemPerPage;
// figures number of pages needed
$numofpages = ceil($totalResults / $limit);
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo $i . " ";
}else{
echo "<a href='gallery.php?page=" . $i . "'>" . $i . "</a> ";
}
}
上面的For循环可以更改为仅显示10并进行一些修改。
for($i = 0; $i <= $numofpages; $i++){
$pageNum = $page;
$currentPageIndex = floor($pageNum/10); // Will return 1 for 11
$pageIndex = ($currentPageIndex * 10) + 1);
if($currentPageIndex > 0) {
$pageIndex = ($currentPageIndex * 10) + 1);
if($pageIndex == $page){
$pagingLink[] = " $pageIndex ";
} else {
$pagingLink[] = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',$pageIndex);return false;\">$page</a> ";
}
} else {
if($i == $page){
$pagingLink[] = " $page ";
} else {
$pagingLink[] = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',$i);return false;\">$page</a> ";
}
}
}
尚未测试修改过的代码。可能需要一些推文