Home

Friday, May 6, 2011

Simple Pagination in PHP

<head>
<title>Simple Pagination in PHP</title>
<style type="text/css">
/*---------------------Pagignation CSS --------------*/
#tnt_pagination {
    display:block;
    text-align:left;
    height:22px;
    line-height:21px;
    clear:both;
    padding-top:3px;
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
    font-weight:normal;


}
#tnt_pagination a:link, #tnt_pagination a:visited{
    padding:7px;
    padding-top:2px;
    padding-bottom:2px;
    border:1px solid #EBEBEB;
    margin-left:10px;
    text-decoration:none;
    background-color:#F5F5F5;
    color:#0072bc;
    width:32px;
    font-weight:normal;
    }
#tnt_pagination a:hover {
    background-color:#DDEEFF;
    border:1px solid #BBDDFF;
    color:#0072BC;   
}
#tnt_pagination .active_tnt_link {
    padding:7px;
    padding-top:2px;
    padding-bottom:2px;
    border:1px solid #BBDDFF;
    margin-left:10px;
    text-decoration:none;
    background-color:#DDEEFF;
    color:#0072BC;
    cursor:default;
}
#tnt_pagination .disabled_tnt_pagination {
    padding:7px;
    padding-top:2px;
    padding-bottom:2px;
    border:1px solid #EBEBEB;
    margin-left:10px;
    text-decoration:none;
    background-color:#F5F5F5;
    color:#D7D7D7;
    cursor:default;
}
.pagination{
padding: 2px;
}
.pagination ul{
margin: 0;
padding: 0;
text-align: left; /*Set to "right" to right align pagination interface*/
font-size: 11px;
padding-top:5px;
height:15px !important;
height:20px;
}
.pagination li{
list-style-type: none;
display: inline;
padding-bottom: 1px;
}
.pagination a, .pagination a:visited{
padding: 0 5px;
border: 1px solid #9aafe5;
text-decoration: none;
color: #2e6ab1;
}
.pagination a:hover, .pagination a:active{
border: 1px solid #2b66a5;
color: #000;
background-color: #FFFF80;
}
.pagination a.currentpage{
background-color: #2e6ab1;
color: #FFF !important;
border-color: #2b66a5;
font-weight: bold;
cursor: default;
}
.pagination a.disablelink, .pagination a.disablelink:hover{
background-color: white;
cursor: default;
color: #929292;
border-color: #929292;
font-weight: normal !important;
}
.pagination a.prevnext{
font-weight: bold;
}
</style>
</head>
<body>
<?php
$con = mysql_connect('localhost','root','') or die("database error:".mysql_error());
if($con)
{
    mysql_select_db("database_name") or die("Database Connection Error.");
}


$query = "SELECT * FROM table_name";
$result = mysql_query($query);
$num_record = mysql_num_rows($result);
   
$total_records = mysql_num_rows($result);//here is the total records
$records_per_page = 1;//how many results per page
$total_pages = ceil($total_records / $records_per_page);//total number of pages
$page = intval($_GET['p']);//current page


if ($page < 1 || $page > $total_pages) $page = 1;//be sure is a number
$offset = ($page - 1) * $records_per_page;//position
                   
$limit = " LIMIT $offset, $records_per_page";//sql we need to add IMPORTANT
   
$sql = "SELECT * FROM table_name $limit";
$rs = mysql_query($sql) or die("Database Error :".mysql_error());


if($num_record > 0 )
{
?>
<p><span style="font-size:12px;">Result</span></p>
<table cellpadding="3" cellspacing="0" border="1" width="90%" align="center">
        <tr>
            <th width="10%" align="center">No.</th>
            <th width="90%">Skill</th>
        </tr>
        <?
        if($_GET['p'])
        {
            if($_GET['p'] == 2)
            {
                $i=$records_per_page;
            }
            else if($_GET['p']==1)
            {
                $i= 0;
            }
            else
            {
                $i= (($_GET['p'] * $records_per_page) - $records_per_page);
            }
        }
        else
        {
            $i = 0;
        }
        while($row=mysql_fetch_array($rs))
        {   
        $i++;
            ?>
            <tr>
                <td align="center"><?= $i; ?>.</td>
                <td><?= $row['column_name']; ?></td>
            </tr>
            <?
            }
        $a = $total_records;
        $b = $records_per_page;//$records_per_page-1;
        if($a>$b)
        {
        ?>
        <tr><td colspan="2">
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr><td align="center">
                    <div class="pagination">
                    <?php   
                    $display_pages=2;//how many pages to display
                    ?><ul><li><?
                    echo "<a title='Start'  href='?p=1' class='prevnext'>Start »</a> ";//Start
                    if ($page>1) echo "</a><a class='prevnext disablelink' title='Previous' href='?p=".($page-1)."'>« Prev</a> "; //Previous
                    
                    for ($i = $page; $i <= $total_pages && $i<=($page+$display_pages); $i++) {
                          if ($i == $page) echo "<li><a class='currentpage' > $i </span>";//not printing the link
                          else echo "</a>&nbsp;<a title='page $i' href='?p=$i'> $i</a>&nbsp;";//link
                    }
                    
                    if (($page+$display_pages)< $total_pages) echo "..."; //etcetera...
                    if ($page<$total_pages) echo "<a title='Next' href='?p=".($page+1)."'>Next »";//Next
                    echo "</a>&nbsp;<a title='End' href='?p=$total_pages' class='prevnext'>End »</a>";//end
                    ?>
                    </div>
                    </td></tr>
                </table>
        </td></tr>
        <?
            }
        ?>
    </table>
<? }
else
{ ?>
<p align="center"><span style="color:#F00;" >No record found.</span>&nbsp;&nbsp;&nbsp;&nbsp;</p>
<? } ?>
</body>

No comments:

Post a Comment

rathoddhirendra.blogspot.com-Google pagerank and Worth