Source Code for Paginations in Php

                    Here again I am introducing one of the important code called Source Code for Paginations in Php. Before discussing about Source Code for Paginations in Php, Here we have to know first what Paginations is so that we can understand clearly about this code.

                    Ok, here I am discussing with small example when you guys go the search engines called Google, Yahoo or some thing else you type some matter in that box and when you click the search button
definitely no. of results displayed . I am shore these results in thousands or in much more but in your browser
at a time 10 -15 displayed and remaining displayed with paginations buttons followed in each bottom of the page and I think you guys understood about the paginations. So why can’t we apply this type of code to our dynamic sites.
                    Mean while when come to the technical part when we write a code for the “Search Button” definitely we use a query in Mysql called “Select” w.r.t this we use paginations code. Hence Source Code For Paginations In Php is as follows :-


<?
include "dbconnect.php";

//Code for pagination
$tbl_name="Your_Table_Name"; //here enter your table name.
$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 = "display_page.php";
$limit = 5; //here enter your limit
$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";
}
?>

No comments: