如何在此代码中进行分页?

时间:2016-09-28 10:24:10

标签: php mysql pagination

<?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>";



?>

1 个答案:

答案 0 :(得分:2)

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

  • 转到https://github.com/rajdeeppaul/Pagination,下载pagination.php文件并将其包含在项目目录中,如下所示:

    require_once('pagination.php');
    
  • 使用适当的驱动程序创建Pagination类的实例,如下所示:

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

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

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

    $pg->setPaginationParameters(10, 5);
    
  • 调用getResult()类的Pagination方法,根据网址?page=X显示行,如下所示,

    $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()方法显示分页链接,如下所示,

    $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与您的信息页。

这是完整的代码:

<?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>";
?>

脚注:如果您需要进一步说明此脚本的使用情况,请浏览使用文档。