0

我有这个用于分页的功能,它设置为在链接中一次只显示 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;\">&laquo; Prev <sp>|</sp></a> ";
            } else {
                $prev = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',$page);return false;\">&laquo; Prev <sp>|</sp></a> ";
            }
            $first = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',1);return false;\">&laquo; 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 &raquo;</a> ";
            $last = " <a class=\"paginglink\" href=\"#\" onclick=\"refreshsearch('$q',$totalPages);return false;\"><sp>|</sp> Last &raquo;</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的加法和减法,但无济于事。提前致谢!对不起,高大的职位

编辑:为了让问题更清楚,当我在每 10 页(10,20,30,...)上时,它会显示下一组十个链接。在第 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> ";
            }
        }
4

1 回答 1

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> ";
        }
    }
}

没有测试修改过的代码。可能需要一些调整

于 2012-07-31T06:34:00.530 回答