如何创建修复分页编号

时间:2014-09-24 09:04:07

标签: php pagination

我找到了分页代码。这是第一次分页三个数字显示,如: [1] 2 3 next>>

如果我点击数字2,它会显示: << prev 1 [2] 3 4 next>>

但如果有超过三页,我需要三个数字才能显示。就像, << prev 1 [2] 3 4 next>>

page.php

mysql_connect("localhost","root","");
mysql_select_db("mydatabase");

$rowsperpage = 4; // how many items per page
$pagerange = 2;// how many pages to show in page link

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;

$qury1 = "SELECT COUNT(*) FROM audit";
$result_page = mysql_query($qury1) or die(mysql_error());
$r = mysql_fetch_row($result_page);
$numrows = $r[0];
$totalpages = ceil($numrows / $rowsperpage);

if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1)  {
   // set current page to first page
   $currentpage = 1;
} // end if

/*PUT YOUR SQL CODE HERE AS WELL AS EVERYTHING THAT SHOULD BE ON THE PAGE.
THE FOLLOWING GOES BELOW AT THE BOTTOM OF YOUR CONTENT:*/

$result_page = mysql_query("SELECT * FROM audit");
$num_rows = mysql_num_rows($result_page);

if ($num_rows<1) {
} else {
    echo "Page ".$currentpage." of ".$totalpages."<br>";
    if ($currentpage > 1) {
        // show << link to go back to page 1
        echo " <a href='page.php?currentpage=1'><<</a> ";
        // get previous page num
        $prevpage = $currentpage - 1;
        // show < link to go back to 1 page
        echo " <a href='page.php?currentpage=$prevpage'>prev</a> ";
    } // end if

    // loop to show links to range of pages around current page
    for ($x = ($currentpage-$pagerange); $x < (($currentpage+$pagerange)+1); $x++) {
        // if it's a valid page number...
        if (($x > 0) && ($x <= $totalpages)) {
            // if we're on current page...
            if ($x == $currentpage) {
                // 'highlight' it but don't make a link
                echo " [<b>$x</b>] ";
                // if not current page...
            } else {
                // make it a link
                echo " <a href='page.php?currentpage=$x'>$x</a> ";
            } // end else
        } // end if
    } // end for

    // if not on last page, show forward and last page links
    if ($currentpage != $totalpages) {
        // get next page
        $nextpage = $currentpage + 1;
        // echo forward link for next page
        echo " <a href='page.php?currentpage=$nextpage'>next</a> ";
        // echo forward link for lastpage
        echo " <a href='page.php?currentpage=$totalpages'>>></a> ";
    } // end if
} // end else

1 个答案:

答案 0 :(得分:0)

您的代码似乎会在$pagerange变量中存储可见页面的数量。在这种情况下,它被设置为2,这导致在当前页面的每一侧显示两个页面。例如,如果我们在第4页上,则链接将为<< prev 2 3 [4] 5 6 next >>

因此,将其设置为1会使可见页面的数量为3:

$pagerange = 1;

链接将为<< prev 3 [4] 5 next >>

但是,当我们在第一页或最后一页时,此设置只会在链接部分显示一个额外的页码:[1] 2 next >>

这需要在第一页和最后一页的情况下额外增加范围:

if ($currentpage == 1 || $currentpage == $totalpages) {
    $pagerange = 2;
}

此段应在$currentpage的所有检查之后但在打印任何链接之前放置。我会把它放在长评/*PUT YOUR SQL CODE HERE...之前。

现在,我们将[1] 2 3 next >>