Example for pagination in Php

                   As I already mentioned the code in my topics called Codings. For more details about paginations visit Source code for paginations in Php in this Blog. Mean while here I am introducing an Example for pagination in Php with a small topic taken from my past project page. Change the table name, no. of pages display for each page, database connection name and so on…

                   Hence here is your code for pagination in Php with example i.e. is as follows written in two steps:-


Step1: Paste the code in the beginning of the page.

<?
include "dbconnect.php";

$tbl_name="news";
$adjacents = 3;
$query ="SELECT count(*) as num FROM $tbl_name";
//echo $query;
$query1=mysql_num_rows(mysql_query($query));
//echo $query1;
$total_pages = mysql_fetch_array(mysql_query($query));
//echo $total_pages[num];
$total_pages = $total_pages[num];
//echo $total_pages;
$targetpage = "news_list.php";
$limit = 5;
$page = $_GET['page'];
//echo $page;
if($page) $start = ($page - 1) * $limit;
else $start = 0;
$sql = "SELECT * FROM $tbl_name ORDER BY `n_id` DESC LIMIT $start, $limit";
//echo $sql;
$result = mysql_query($sql);
//echo $result['bname'];
if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
if ($page > 1)
$pagination.= "«<a href=\"$targetpage?page=$prev\">previous</a>&nbsp;";
else
$pagination.= "«<span class=\"disabled\">previous</span>&nbsp;";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>&nbsp;&nbsp;";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>&nbsp;&nbsp;";
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
echo ' ';
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}
if ($page < $counter - 1)
$pagination.= "&nbsp;<a href=\"$targetpage?page=$next\">next</a> »";
else
$pagination.= "&nbsp;<span class=\"disabled\"> next</span> »";
$pagination.= "</div>\n";
}
?>

Step2: Paste the code with continues to step1 or after following to step1.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>:: Welcome To Checkboxesinphp ::</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="my.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style18 {color: #660033}
.style24 {font-weight: bold; font-family: Verdana, Arial, Helvetica, sans-serif; color: #660000;}
.style25 {color: #4D6DF4}
</style>
</head>

<body>
<table width="801" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#000099">
<tr>
<td valign="top"><table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><? include"header.php";?></td>
</tr>
<tr>
<td bgcolor="#FFFFFF"><? include"menulinks.php";?></td>
</tr>
<tr>
<td height="300" valign="top" bgcolor="#FFFFFF"><table width="100%" height="99%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<table width="99%" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="1">&nbsp;</td>
</tr>
<form method="post" name="login" id="login" onsubmit="return check()">
<tr>
<td><div align="center" class="subcon">
<p>&nbsp;</p>
<h3 class="heading style25">ALL NEWS LIST </h3>
<table width="73%" height="140" border="0" cellpadding="0" cellspacing="0">
<tr>
<th width="45%" height="32" scope="col"><span class="style25">TITLE</span></th>
<th width="33%" scope="col"><span class="style25">EDIT NEWS </span></th>
<th width="33%" scope="col"><span class="style25">DELETE NEWS </span></th>
</tr>
<?php

$num=mysql_num_rows($result);

if($num=='')
{
?>
<tr>
<td colspan="10" align="center"><div align="center"><? echo "no records found"; ?></div></td>
</tr>
<?
}
else
{
while($fel=mysql_fetch_array($result))
{

?>
<tr>
<td><div align="center"><strong class="style24">
<?=$fel['title']?>
</strong></div></td>
<td><div align="center"><a href="news_edit.php?id=<?=$fel['n_id']?>"><img src="images/b_edit.png" border="0" width="16" height="16"/></a>&nbsp;&nbsp;<font color="#660033">&nbsp;<strong class="style18">EDIT</strong></font></div></td>
<td width="22%"><div align="center"><a href="news_del.php?id=<?=$fel['n_id']?>"><img src="images/b_drop.png" border="0" /></a>&nbsp;<strong><span class="style18">&nbsp;DELETE</span></strong></div></td>
</tr>
<tr>
<td colspan="10" align="center">&nbsp;</td>
</tr>
<? }
?>
<h3 align="right">
<?=$pagination?>
</h3>
<? }?>
<!--<tr>
<td colspan="10" align="center">&nbsp;</td>
</tr>-->
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div></td>
</tr></form>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
</tr>
<tr>
<td><? include"footer.php";?></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>

No comments: