如何在此代码中进行分页? [英] how to do pagination in this code?

查看:69
本文介绍了如何在此代码中进行分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
        include("config/connection.php");
        $qry="SELECT * FROM `students_info`";
        $qry_sel=mysqli_query($con,$qry);
            echo "<table border='2'>";
            echo"<tr>";
            echo "<td>SL_NO</td>";
            echo "<td>NAME</td>";
            echo "<td>ROLL</td>";
            echo "<td>COLLEGE_ID</td>";
            echo "<td>CLASS</td>";
            echo "<td>STATUS</td>";

            echo "</tr>";
            while($row=mysqli_fetch_array($qry_sel))
            {
                echo "<tr>";
                $slno=$row['sl_no'];
                echo "<td>".$row['sl_no']."</td>";
                echo "<td>".$row['name']."</td>";
                echo "<td>".$row['roll']."</td>";
                echo "<td>".$row['clgid']."</td>";
                echo "<td>".$row['class']."</td>";
                echo "<td><a href='delete.php?id=$slno'>DELETE</a><a href='edit.php?id=$slno'>!!EDIT</a></td>";

                //echo"<td>"."<a href='delete.php?del=".$id."'>Delete</a>"."</td>";
                echo "</tr>";
            }
            echo"</table>";



?>

推荐答案

以下步骤将指导您如何在现有代码中实现和合并分页.

The following steps will guide you on how to implement and incorporate pagination in your existing code.

  • Go to https://github.com/rajdeeppaul/Pagination, download pagination.php file and include it into your project directory, like this:

require_once('pagination.php');

  • 使用适当的驱动程序创建Pagination类的实例,如下所示:

  • Create an instance of Pagination class, with appropriate driver, like this:

    $pg = new Pagination('mysqli', 'HOSTNAME', 'USERNAME', 'PASSWORD', 'DATABASE_NAME');
    

    根据您的数据库凭据更改HOSTNAMEUSERNAMEPASSWORDDATABASE_NAME.

    Change HOSTNAME, USERNAME, PASSWORD and DATABASE_NAME as per your database credentials.

    使用setPaginationParameters()方法设置分页参数,如下所示,

    Set pagination parameters using setPaginationParameters() method, like this,

    $pg->setPaginationParameters(10, 5);
    

  • 调用Pagination类的getResult()方法以显示基于URL查询?page=X的行,像这样

  • Call getResult() method of Pagination class to display rows based on the URL query ?page=X, like this,

    $resultSet = $pg->getResult('SELECT * FROM students_info', NULL, $_GET, 'page');
    
    foreach($resultSet as $row){
        echo "<tr>";
            $slno=$row['sl_no'];
            echo "<td>".$row['sl_no']."</td>";
            echo "<td>".$row['name']."</td>";
            echo "<td>".$row['roll']."</td>";
            echo "<td>".$row['clgid']."</td>";
            echo "<td>".$row['class']."</td>";
            echo "<td><a href='delete.php?id=" . $slno . "'>DELETE</a><a href='edit.php?id=" . $slno . "'>!!EDIT</a></td>";
        echo "</tr>";
    }
    

  • 最后,使用getPaginationLinks()方法显示分页链接,像这样

  • Finally, display pagination links using getPaginationLinks() method, like this,

    $pgLinks = $pg->getPaginationLinks();
    if(is_array($pgLinks) && count($pgLinks) && $pgLinks['prev']){
        /* previous pages are available */
        echo '&laquo; ';
    }
    if(is_array($pgLinks) && count($pgLinks) && count($pgLinks['links'])){
        /* show pagination links */
        foreach($pgLinks['links'] as $link){
            echo '<a href="yourPage.php?page='.$link.'">'.$link.'</a> ';
        }
    }
    if(is_array($pgLinks) && count($pgLinks) && $pgLinks['next']){
        /* next pages are available */
        echo '&raquo;';
    }
    

    注意:不要忘记在页面上更改yourPage.php.

    Note: Don't forget to change yourPage.php with your page.

    以下是完整的代码:

    <?php
        require_once('pagination.php');
    
        $pg = new Pagination('mysqli', 'HOSTNAME', 'USERNAME', 'PASSWORD', 'DATABASE_NAME');
        $pg->setPaginationParameters(10, 5);
    
        $resultSet = $pg->getResult('SELECT * FROM students_info', NULL, $_GET, 'page');
    
        echo "<table border='2'>";
            echo"<tr>";
            echo "<td>SL_NO</td>";
            echo "<td>NAME</td>";
            echo "<td>ROLL</td>";
            echo "<td>COLLEGE_ID</td>";
            echo "<td>CLASS</td>";
            echo "<td>STATUS</td>";
    
            echo "</tr>";
            foreach($resultSet as $row){
                echo "<tr>";
                    $slno=$row['sl_no'];
                    echo "<td>".$row['sl_no']."</td>";
                    echo "<td>".$row['name']."</td>";
                    echo "<td>".$row['roll']."</td>";
                    echo "<td>".$row['clgid']."</td>";
                    echo "<td>".$row['class']."</td>";
                    echo "<td><a href='delete.php?id=" . $slno . "'>DELETE</a><a href='edit.php?id=" . $slno . "'>!!EDIT</a></td>";
                echo "</tr>";
            }
    
            $pgLinks = $pg->getPaginationLinks();
            echo "<tr style='text-align:center;'><td colspan='6'>"; 
            if(is_array($pgLinks) && count($pgLinks) && $pgLinks['prev']){
                /* previous pages are available */
                echo '&laquo; ';
            }
            if(is_array($pgLinks) && count($pgLinks) && count($pgLinks['links'])){
                /* show pagination links */
                foreach($pgLinks['links'] as $link){
                    echo '<a href="yourPage.php?page='.$link.'">'.$link.'</a> ';
                }
            }
            if(is_array($pgLinks) && count($pgLinks) && $pgLinks['next']){
                /* next pages are available */
                echo '&raquo;';
            }
            echo "</td></tr>";
        echo"</table>";
    ?>
    


    脚注::如果需要进一步说明此脚本的用法,请阅读用法文档.


    Footnotes: Go through the Usage documentation if you need any further clarification on the usage of this script.

    这篇关于如何在此代码中进行分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆