<?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:
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');
Create an instance of
Pagination
class, with appropriate driver, like this:$pg = new Pagination('mysqli', 'HOSTNAME', 'USERNAME', 'PASSWORD', 'DATABASE_NAME');
Change
HOSTNAME
,USERNAME
,PASSWORD
andDATABASE_NAME
as per your database credentials.Set pagination parameters using
setPaginationParameters()
method, like this,$pg->setPaginationParameters(10, 5);
Call
getResult()
method ofPagination
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>"; }
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 '« '; } 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 '»'; }
Note: Don't forget to change
yourPage.php
with your page.
Here's the complete code:
<?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 '« ';
}
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 '»';
}
echo "</td></tr>";
echo"</table>";
?>
Footnotes: Go through the Usage documentation if you need any further clarification on the usage of this script.