繁体   English   中英

所有分页号均使用PHP和Mysql显示

[英]All Pagination numbers are displaying using PHP and Mysql

我正在从数据库中获取用户名和ID。 我有500多个用户名。 我必须仅显示1到5的分页号和最后一个分页号。

示例:-分页号是:-1 2 3 4 5 ... 20(最后一个数字)。

现在,我得到的所有数字都是水平的。 您能帮我吗?

有人可以在分页中使用NEXT和LAST帮助我吗?

include('../db/connection.php');
    $reclimit = 3;
     if(isset($_GET['page'])){
        $page = $_GET['page'];
    } else {
        $page = 1;
    }

    $start = (($page-1) * $reclimit);
    $sql = "SELECT * FROM request";

    $records =$conn->query($sql);;
    $total = $records->num_rows;
    $tpages = ceil($total / $reclimit);

    $search_sql="SELECT * FROM request LIMIT ".$start."," .$reclimit; 
    $search_result = $conn->query($search_sql); 

HTML

 <body>
    <?php 

        if (isset($search_result->num_rows) > 0) {
        ?>
        <h2 class="result-title">Results matching your need</h2>
        <?php
            while($search_ok = $search_result->fetch_assoc()) {
                    $user_id=$search_ok['Id'];
                    $user_name=$search_ok['Name'];

        echo "
            <div class='search-section'>
                    <div class='search-profile'>

                    <div class='s_user_id'>{$user_id}</div>
                    <div class='s_user_name'>{$user_name}</div>

                    </div>
                    </div>
        ";

         }}
      for($i=1;$i<=$tpages;$i++) {
                        echo "<a href=page.php?page=".$i.">".$i."</a>";
                    }
                ?>



    </body>

您正在使用的分页简单而又有效。 但是,您正在搜索的分页是明智的方法,您应该通过使用一些if条件来实现此目的。 SO中也有类似的答案。 前往以下网址,这可能对您有帮助

这是一个简单的想法,但我没有对其进行测试。 编辑foreach显示数字:

$pgStart = 1;
if (isset($_GET['page'])) { // get first showing number = current page - 2
    $pg = $_GET['page'] - 2;
    $pgStart = $pg + 5 > $tpages ? $tpages - 4 : $pg; //EDIT fix when reach pages end
    $pgStart = $pg < 1 ? 1 : $pg; // This must be after ending correction (previous line)
}

if ($pgStart > 1) { // show 1
    echo '<a href=page.php?page="1">1</a> ... ';
}

for($i = $pgStart; $i <= $tpages && $i < $pgStart + 5; $i++) { // show 5 pages
    echo ' <a href=page.php?page="'.$i.'">'.$i.'</a> ';
}

if ($i < $tpages) { // show last
    echo ' ... <a href=page.php?page="'.$tpages.'">'.$tpages.'</a>';
}

编辑

此脚本的$_GET['page'] = 7$tpages = 20来自php沙箱的输出是(没有换行符):

<a href=page.php?page="1">1</a> ... 
<a href=page.php?page="5">5</a> 
<a href=page.php?page="6">6</a> 
<a href=page.php?page="7">7</a> 
<a href=page.php?page="8">8</a> 
<a href=page.php?page="9">9</a> 
... <a href=page.php?page="20">20</a>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM