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条)

报告相同问题?

悬赏问题

  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令