doutong6814 2015-04-30 10:16
浏览 65
已采纳

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

I have a code that helps in 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>

code for 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>
";
    } elseif ($page == 2) {
        $out.= "<li><a  href=\"" . $reload . "\">" . $prevlabel . "</a>
</li>";
    } else {
        $out.= "<li><a  href=\"" . $reload . "&amp;page=" . ($page - 1) . "\">" . $prevlabel . "</a>
</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>
";
        } elseif ($i == 1) {
            $out.= "<li><a  href=\"" . $reload . "\">" . $i . "</a>
</li>";
        } else {
            $out.= "<li><a  href=\"" . $reload . "&amp;page=" . $i . "\">" . $i . "</a>
</li>";
        }
    }

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

Everything is working properly without any problem but I would like to add one more feature.

currently I am getting pagination like this

enter image description here

I would like to add first and last button before prev and after next respectively. These buttons should be present at all times except on first and last page.

can anyone tell how I can do so?

  • 写回答

2条回答 默认 最新

  • dousong3760 2015-04-30 10:20
    关注

    In paginate.php change on 6th line

    $out = "";
    

    for

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

    And before return add the last page link:

    $out.= "<li><a  href=\"" . $reload . "&amp;page=" . $tpages . "\">LAST PAGE</a>
    </li>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验