Pages

Tuesday, October 18, 2011

PHP Paging script

$page = isset($_REQUEST['page_no']) ? $_REQUEST['page_no'] : 1;
$show = 10;
$start = ($page - 1) * $show;

$result = mysql_query("SQL QUERY");

$total = mysql_num_rows($result);
$total_page = ceil($total / $show);

$res = mysql_query("SQL QUERY limit $start,$show");

$first = $start + 1;
$last = $first + $show;
if($last > $total)
{   
    $last = $total;
}
if(mysql_num_rows($res) > 0)
{
    while($row = mysql_fetch_array($res))
    {   }
}

/**********     Page Links  **********/
<?php   
    $width = 4;
    if($total > $show)
    {
    if($page >= 1){
    print " <a href='YOURPAGE.php?page_no=1' class='page-numbers' >First</a> ";}
       
    if(($page-$width) > 1)
    {       
            for($i=($page-$width); $i<$page ;$i++)
            {
                print " <a href='YOURPAGE.php?page_no=$i' class='page-numbers' >$i</a> ";
            }
    }
    else
    {
                for($i=1; $i<$page ;$i++)
            {
                print " <a href='YOURPAGE.php?page_no=$i' class='page-numbers' >$i</a> ";
            }
        }
    print "<span class='page-numbers current'  >".$page."</span>";
    if(($page+$width) < ($total_page-1))
    {
        for($i=($page+1); $i<= ($page + $width); $i++)
        {
            print " <a href='YOURPAGE.php?page_no=$i' class='page-numbers' >".$i."</a> ";
        }       
    }
    else
    {
        for($i=($page+1); $i<= ($total_page); $i++)
        {   
            print " <a href='YOURPAGE.php?page_no=$i' class='page-numbers' >".$i."</a> ";
                  }   
    }
   
    if($page < $total_page)
    print "<font size='2'><a href='YOURPAGE.php?page_no=$total_page' class='page-numbers' >Last</a></font> ";
    else
    print "<font size='2'>Last</font>";
    }
    ?>
</div>






1 comment:

Anonymous said...

Hi,
Thanks, Its very usefull post