php 如何限制分页中显示的数字?

ryoqjall  于 2023-04-28  发布在  PHP
关注(0)|答案(2)|浏览(134)

这是一个学校项目,该页面显示了一个表格,该表格分为每页15个部分,但在表格的底部,它们是要转到哪一页的数字。
我的表有862450行,这意味着底部显示了57408个数字。我该如何将它限制在5个数字,使它看起来像这样
〈< 12345 >〉
任何帮助都非常感谢!

<div class="container mt-4">

      

        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h4>List of Regulated Pools
                      
                        </h4>
                        <div class="btn-group">
  <a href="Databaseone.php" class="btn btn-primary active" aria-current="page"> Regulated Pool Register</a>
  <a href="Databasetwo.php" class="btn btn-primary">Certified Safety Pools</a>
  <a href="Databasethree.php" class="btn btn-primary">Registered Pool Inspectors</a>
</div>
                    </div>
                    <div class="card-body">

                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Site Name</th>
                                    <th>Unit Number</th>
                                    <th>Street Number</th>
                                    <th>Street Name</th>
                                    <th>Street Type</th>
                                    <th>Suburb</th> 
                                     <th>Post Code</th> 
                                    <th>Nubmer of Pools</th>
                                    <th>Local Goverment Authority</th>
                                    <th>Shared Pool</th>
                                  
                                </tr>
                            </thead>
                            <tbody>
                            
                                <?php
       
                                require('conn.php');
                                $num_per_page=15;
                                
                                if(isset($_GET["page"]))
                                {
                                    $page=$_GET["page"];
                                }
                                else
                                {
                                    $page=1;
                                }
                                
                                $start_from=($page-1)*15;
                                
                               
                                $query="select * from norm_pool limit $start_from,$num_per_page";
                                $result= mysqli_query($conn, $query);
                                    
                                  
                                    
                                    if(mysqli_num_rows($result) > 1 )
                                    {
                                            while($row = $result->fetch_assoc()) 
                                        {
                                            ?>
                                            <tr>
                                                 <td><?= $row['ID']; ?></td>
                                                <td><?= $row['sitename']; ?></td>
                                                <td><?= $row['unitno']; ?></td>
                                                <td><?= $row['streetno']; ?></td>
                                                <td><?= $row['streetname']; ?></td>
                                                <td><?= $row['streettype']; ?></td>
                                                <td><?= $row['suburb']; ?></td>
                                                <td><?= $row['postcode']; ?></td>
                                                <td><?= $row['noofpools']; ?></td>
                                                <td><?= $row['localgovauth']; ?></td>
                                                <td><?= $row['sharedpool']; ?></td>
                                        
                                               
                                            </tr>
                                            <?php
                                        }
                                    }
                                    else
                                    {
                                        echo "<h5> No Users Found </h5>";
                                    }
                                 
                                ?>
                                
                                
                            </tbody>
                        </table>
                        <?php 
                        require('conn.php');
                                $query='select * from norm_pool';
                                $result= mysqli_query($conn, $query);
                        $total_records=mysqli_num_rows($result);
                        $total_pages=ceil($total_records/$num_per_page);
                        
                        for($i=1;$i<=$total_pages;$i++)
                        {
                            echo "<a href='Databaseone.php?page=".$i."'>".$i."</a>";
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

我已经尝试了多种解决方案,没有一个成功,因为所有的解决方案都有非常不同的代码给我

e1xvtsh3

e1xvtsh31#

这里有一个方法
请看下面的代码:

//get the current page
$pagenumber = $_GET['page'];

                        $query='select * from norm_pool';
                        $result= mysqli_query($conn, $query);

                      
                        $total_records=mysqli_num_rows($result);
                       
                        $total_pages=ceil($total_records/$num_per_page);
                         
                        //link for the first page
                        echo "<a class='btn btn-warning m-2' href='Databaseone.php?page=1'> < First Page > </a>";

                        //As we want only 5 link in pagination hence we can itrate it only 5 times
                        for($i=1;$i<=5;$i++)
                        {
                            //incrementing page number before because we do not want to include current page.
                            $pagenumber++;
                            echo "<a class='btn btn-primary m-2' href='Databaseone.php?page=".$pagenumber."'>".$pagenumber."   </a>";

                        }
    //link for the last page
                        echo "<a class='btn btn-danger m-2' href='Databaseone.php?page=$total_pages'> < Last Page > </a>";

结果:(我相信你使用bootstrap,所以我添加了一些)

liwlm1x9

liwlm1x92#

要限制分页中显示的数字,您可以使用“省略号”或“点”的概念来指示除了当前显示的页面之外还有更多的页面可用。以下是您可以遵循的一些步骤:
1.确定在任何给定时间要显示的最大页数。
1.计算当前页面相对于总页数的位置。
1.如果当前页靠近分页的开头或结尾,则显示允许的最大页数。
1.如果当前页面位于中间,则显示允许的最大页数,用省略号或圆点表示还有更多可用页面。
1.当用户单击省略号时,更新分页以显示下一组页面。
例如,假设您希望在任何给定时间最多显示5页,并且您当前在总共20页中的第10页。分页将显示如下内容:
12 ... 910 11 .十九二十
省略号表示除了当前显示的页面之外还有更多可用的页面,当用户单击它时,分页将更新以显示下一组页面。

相关问题