Pagination using core PHP (without using jQuery or AJAX)

<!-- pagination start -->
<?php
// number of results to show per page
$per_page = 20;

// figure out the total pages in the database
$result = mysql_query("SELECT * FROM tbl_student_register ORDER BY prn_no DESC");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);

// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if(isset($_GET['page']) && is_numeric($_GET['page'])) {
$show_page = $_GET['page'];

// make sure the $show_page value is valid
if($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 0;
$end = $per_page;
}              
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}

// display data in table
echo "<div class='well' id='soc'>
<table width='96%' class='table' border='1'>";
echo "<thead>
<tr>
<th class='tablehead' width='64' height='39'><center><b>Sr No</b></center> </th>
<th class='tablehead' width='69' height='39'><center><b>Roll No</b></center> </th>
<th class='tablehead' width='233'><center><b>Student Name</b></center></th>
<th class='tablehead' width='119'><center><b>Standard</b></center></th>
<th class='tablehead' width='119'><center><b>Division</b></center></th>
<th class='tablehead' width='211'><center><b>Address</b></center></th>
<th colspan='2' class='tablehead' width='82'><center><b>Action</b></center></th>
</tr>
</thead>
<tbody>";
// loop through results of database query, displaying them in the table
for($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }

// echo out the contents of each row into a table
echo "<tr>";
echo '<td style="font-size:1.3em"><center>' .($i+1). '</center></td>';
echo '<td style="font-size:1.3em"><center>' . mysql_result($result, $i, 'rollno') . '</center></td>';
echo '<td style="font-size:1.3em">' . mysql_result($result, $i, 'stud_fname') .' '. mysql_result($result, $i, 'stud_lname') . '</td>';
echo '<td style="font-size:1.3em">' . mysql_result($result, $i, 'standard') . '</td>';
echo '<td style="font-size:1.3em">' . mysql_result($result, $i, 'division') . '</td>';
echo '<td style="font-size:1.3em">' . mysql_result($result, $i, 'address') . '</td>';
//echo '<td><a href="edit.php?id=' . mysql_result($result, $i, 'rollno') . '">Edit</a>';
echo '<td style="font-size:1.3em; width:10%;"><center><input type="image" src="img/edit.jpg" style="width:17%;" value=' . mysql_result($result, $i, 'prn_no') . ' onClick="modify(this.value);" >';
echo '&nbsp;&nbsp;<input type="image" value=' . mysql_result($result, $i, 'prn_no') . ' onClick="del(this.value);" src="img/delete.jpg" style="width:26%;"></center></td>';
echo "</tr>";
}
// close table>
echo "</tbody></table>";

// pagination

// display pagination

//echo "<p><a href='view.php'>View All</a> | <b>View Page:</b> ";
echo "<center><div class='pagination'><ul><li>";

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

echo "</li></ul></div></center>";
echo "</p></div>";
?>
<!-- pagination end -->




Comments