simple pagination in php , code for beginners

By | June 5, 2017

Simple Pagination With PHP & MYSQL,Creating Paging using PHP and MySQL,How to Create a Pagination in PHP,Perfect PHP Pagination Article

Pagination is one of the common tasks we have in website. This tutorial is for beginners who want to make pagination with PHP and MYSQL. Pagination is one of the best way to display thousands of result into single page.

here is simple code for you :

<?php
$cleaned = clean($_GET);
$num_rec_per_page=10;
mysql_connect('localhost','root','');
mysql_select_db('dummy_db');
if (isset($cleaned["p_id"])) { $p_id = $cleaned["p_id"]; } else { $p_id=1; };
$start_from = ($p_id-1) * $num_rec_per_page;
$sql = "SELECT * FROM post LIMIT $start_from, $num_rec_per_page";
$ps_result = mysql_query ($sql);
?>
<table>
<tr><td>Title</td><td>Image</td></tr>
<?php
while ($row = mysql_fetch_assoc($ps_result)) {
?>
<tr>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['img']; ?></td>
</tr>
<?php
};
?>
</table>
<?php
$sql = "SELECT * FROM post";
$ps_result = mysql_query($sql);
$total_post = mysql_num_rows($ps_result); //count number of total post
$total_post_pages = ceil($total_post / $num_rec_per_page);

echo "<a href='post.php?p_id=1'>".'|<'."</a> "; // Goto 1st page all post (Home)

for ($i=1; $i<=$total_post_pages; $i++) {
echo "<a href='post.php?p_id=".$i."'>".$i."</a> ";
}; // number of post pages
echo "<a href='post.php?p_id=$total_post_pages'>".'>|'."</a> "; // Goto last page of post
?>

 

Leave a Reply

Your email address will not be published. Required fields are marked *