在分页栏上添加第一个和最后一个按钮

时间:2015-04-30 10:16:20

标签: php phpmyadmin pagination

我有一个有助于分页的代码。

的index.php:

<?php
$per_page = 10;         // number of results to show per page

$result = mysqli_query($con,"SELECT * FROM vendor ");
<?
$total_results = mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have

//-------------if page is setcheck------------------//
if (isset($_GET['page'])) {
    $show_page = $_GET['page'];             //it will telles the current page
    if ($show_page > 0 && $show_page <= $total_pages) {
        $start = ($show_page - 1) * $per_page;
        $end = $start + $per_page;
    } else {
        // error - show first set of results
        $start = 0;              
        $end = $per_page;
    }
} else {
    // if page isn't set, show first set of results
    $start = 0;
    $end = $per_page;
}
// display pagination
$page = intval($_GET['page']);

$tpages=$total_pages;
if ($page <= 0)
    $page = 1;
?>

<div id="tab1" class="tab_content">
    <?php
        $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;

            echo "<table class='tablesorter' cellspacing='0'> ";
                echo "<thead>
                            <tr> 
                                <th></th>
                                <th>ID</th> 
                                <th>Shopname</th> 
                                <th>Instagram account</th>
                            </tr> 
                      </thead>";

                    for ($i = $start; $i < $end; $i++) 
                        {
                            if ($i == $total_results) 
                                {
                                    break;
                                }

                                mysqli_data_seek($result, $i);

                                $data = mysqli_fetch_assoc($result);
                                echo "<tr " . $cls . ">";
                                    echo '<td></td>';
                                    echo '<td>' . $data['id'] . '</td>';
                                    echo '<td>' . $data['fullname_shopname'] . '</td>';
                                    echo '<td>' . $data['username_instagramacnt'] . '</td>';

                                echo "</tr>";
                        }        
            echo "</table>";

            echo '<div class="pagination" style="margin-left:300px;" >';
                    if ($total_pages > 1) 
                        {
                            echo paginate($reload, $show_page, $total_pages);
                        }
                echo "</ul>
            </div>";
        ?>
</div>

paginate.php的代码

<?php
function paginate($reload, $page, $tpages) {
    $adjacents = 2;
    $prevlabel = "&lsaquo; Prev";
    $nextlabel = "Next &rsaquo;";
    $out = "";
    // previous
    if ($page == 1) {
        $out.= "<span>" . $prevlabel . "</span>\n";
    } elseif ($page == 2) {
        $out.= "<li><a  href=\"" . $reload . "\">" . $prevlabel . "</a>\n</li>";
    } else {
        $out.= "<li><a  href=\"" . $reload . "&amp;page=" . ($page - 1) . "\">" . $prevlabel . "</a>\n</li>";
    }

    $pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
    $pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
    for ($i = $pmin; $i <= $pmax; $i++) {
        if ($i == $page) {
            $out.= "<li  class=\"active\"><a href=''>" . $i . "</a></li>\n";
        } elseif ($i == 1) {
            $out.= "<li><a  href=\"" . $reload . "\">" . $i . "</a>\n</li>";
        } else {
            $out.= "<li><a  href=\"" . $reload . "&amp;page=" . $i . "\">" . $i . "</a>\n</li>";
        }
    }

    if ($page < ($tpages - $adjacents)) {
        $out.= "<a style='font-size:11px' href=\"" . $reload . "&amp;page=" . $tpages . "\">" . $tpages . "</a>\n";
    }
    // next
    if ($page < $tpages) {
        $out.= "<li><a  href=\"" . $reload . "&amp;page=" . ($page + 1) . "\">" . $nextlabel . "</a>\n</li>";
    } else {
        $out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
    }
    $out.= "";
    return $out;
}

一切正常,没有任何问题,但我想再增加一个功能。

目前我正在加入这样的分页

enter image description here

我想分别在prev和after之后添加第一个和最后一个按钮。除第一页和最后一页外,这些按钮应始终存在。

任何人都可以告诉我该怎么做吗?

2 个答案:

答案 0 :(得分:0)

paginate.php第6行更改

$out = "";

$out = "<li><a  href=\"" . $reload . "&amp;page=1\">FIRST PAGE</a>\n</li>";
// or maybe without page param for the first page?

return之前添加last page link

$out.= "<li><a  href=\"" . $reload . "&amp;page=" . $tpages . "\">LAST PAGE</a>\n</li>";

答案 1 :(得分:0)

只需添加一个js文件&#34; jpages.js&#34;

来自链接 http://luis-almeida.github.io/jPages/table.html

并先设置值:false为&#34; First&#34;最后:false到&#34;最后&#34;