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> ";
else
$pagination.= "«<span class=\"disabled\">previous</span> ";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span> ";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a> ";
}
}
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.= " <a href=\"$targetpage?page=$next\">next</a> »";
else
$pagination.= " <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"> </td>
</tr>
<form method="post" name="login" id="login" onsubmit="return check()">
<tr>
<td><div align="center" class="subcon">
<p> </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> <font color="#660033"> <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> <strong><span class="style18"> DELETE</span></strong></div></td>
</tr>
<tr>
<td colspan="10" align="center"> </td>
</tr>
<? }
?>
<h3 align="right">
<?=$pagination?>
</h3>
<? }?>
<!--<tr>
<td colspan="10" align="center"> </td>
</tr>-->
</table>
<p> </p>
<p> </p>
<p> </p>
</div></td>
</tr></form>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td bgcolor="#FFFFFF"> </td>
</tr>
<tr>
<td><? include"footer.php";?></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
No comments:
Post a Comment