Basic pagination example in PHP with MySQL


Below specified basic pagination in PHP with MySQL

<?php
$cou_per_page=10;
mysql_connect(‘localhost’,’root’,”);
mysql_select_db(‘pagination’);
if (isset($_GET[“page”])) { $page = $_GET[“page”]; } else { $page=1; };
$start_page = ($page-1) * $cou_per_page;
$sql = “SELECT * FROM page_table LIMIT $start_page, $cou_per_page”;
$rs_result = mysql_query ($sql); //run the query
?>
<table>
<tr><td>Name</td><td>Address</td></tr>
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<tr>
<td><?php echo $row[‘page_name’]; ?></td>
<td><?php echo $row[‘page_add’]; ?></td>
</tr>
<?php
};
?>
</table>
<?php
$sql = “SELECT * FROM page_table”;
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $cou_per_page);

echo “<a href=’?page=1′>”.’|<‘.”</a> “; // Goto 1st page

for ($i=1; $i<=$total_pages; $i++) {
echo “<a href=’?page=”.$i.”‘>”.$i.”</a> “;
};
echo “<a href=’?page=$total_pages’>”.’>|’.”</a> “; // Goto last page
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s