Creating boxed Paging in Php / Mysql

Here is where i reveal my source code to the Open Source World and trying to give back something from whatever i have learned.

Starting with the very basis but important part "Box paging" as i searched and searched and searched to get the proper result but failed to get the expected one. By combining logic and the parts from my research here is what i am returning something to all of you which i suppose will definitely help everyone.

The way google does it , the paging gets listed at the bottom with numbers and jumping on any page is easier , on the front end many developers wish to have this kind of paging , here is  my try to help all of them who need it.

You can put the main function in the class and call in from there.
You can change the code to make it work with AJAX easily with a simple javascript ajax function calling on the anchor tags.
 











Here is the code snippet.


<?php 
//************* Connection Starts Here *******************************
    //$dblink=mysql_connect($dbhost,$dbusername,$dbpassword)or die("Database connection failed!!!");
    include("connect.php");
    $num=4; // Total number of pages to be listed in a group
    //************ Connection Ends Here **********************************
   
    //*********************** Functions List ****************************
    function listCategoriesFun($Pg,$dblink) // For Taking Paging based data from database
    {
        $rw_arr=array();
        list($min,$max,$Pg)=PagingFun($Pg);

        $query="select * from tblcategory where status!=1 and parentid='0'";
        $result=mysql_query($query,$dblink);
        $Ttl=mysql_num_rows($result);        $TtlPg=ceil($Ttl/$max);
        if(($TtlPg<$Pg) && ($TtlPg>0)) list($min,$max,$Pg)=PagingFun($Pg-1);

        $QryGtAlSbmsn="select * from tblcategory where status!=1 and parentid='0' order by id desc limit $min,$max";
        $ResGtAlSbmsn=mysql_query($QryGtAlSbmsn,$dblink);
        if(mysql_num_rows($ResGtAlSbmsn)>0)
        {
            while($RwGtAlSbmsn=mysql_fetch_array($ResGtAlSbmsn,MYSQL_ASSOC))
            {
                array_push($rw_arr,$RwGtAlSbmsn);
            }
        }
        return array($TtlPg,$Pg,$rw_arr);
    }

    function PagingFun($Pg) // Paging Function For database query
    {
        global $MxAlw;
        if(strlen($Pg)<=0)    $Pg=1;
        $max=($Pg*$MxAlw);    if($Pg==1) $min=0; else $min=$max-$MxAlw;
        return    array($min,$MxAlw,$Pg);
    }

    function calPage($Pg,$TtlPg) //The Main function to change the paging to google type paging
    {
        $num=4;
        $StartPg=($Pg-($Pg%$num))/$num;
        $EndPg=($Pg-($Pg%$num)+$num)/$num;
        $PrvPg=$StartPg*$num-($num-1);
        if($EndPg*$num>$TtlPg)    {    $LastPg=$TtlPg;    }else{    $LastPg=$StartPg*$num+$num;    }
        $startQ=$StartPg*$num;
        return array($StartPg,$LastPg,$startQ,$EndPg,$PrvPg);
    }
    //********************************** End Of Function list **********************

    $Pg=$_GET["pg"];    if(strlen($Pg)<=0)        $Pg=$_POST["pg"];
    if($Pg<=0 || !is_numeric($Pg))    $Pg=1;
   
    list($TtlPg,$Pg,$rw_arr)=listCategoriesFun($Pg,$dblink);

    // Calculate the number paging
    list($StartPg,$LastPg,$startQ,$EndPg,$PrvPg)=calPage($Pg,$TtlPg);

?>
<table border="1" cellspacing="2" cellpadding="2">
    <tr><td>ID</td><td>Catname</td></tr>
    <? for($i=0;$i<count($rw_arr);$i++)
    { ?>
    <tr><td><? echo $rw_arr[$i]["id"]; ?></td><td><? echo $rw_arr[$i]["catname"]; ?></td></tr>
    <? } ?>
</table>

<div>
<?php
    if($TtlPg>1)
    { ?>
        <?php if($StartPg>=1 ){?>
        <span>
            <a href="<?php echo "testpaging.php?pg=".$PrvPg; ?>"  class="SignUpTextblueBold">Prev&nbsp;<? echo $num;?></a>
        </span>
        <?php }
        for($i=$startQ;$i<=$LastPg;$i++)
        {               
            if($i!=0){ ?>
            <span>                                                               
                <? if($i!=$Pg){ ?><a href="<?php echo "testpaging.php?pg=".$i; ?>" ><? } ?><?php echo $i;?><? if($i!=$Pg){ ?></a><? } ?>
            </span>
        <?php    }
        }
        $NxtPg=$LastPg+1;
        if($NxtPg>$TtlPg) $NxtPg=$TtlPg;
       
        if($EndPg < ceil($TtlPg/$num)){ ?>
        <span>
            &nbsp;<a href="<?php echo "testpaging.php?pg=".$NxtPg."&sbmsnid=".$SbmsnId; ?>"  class="SignUpTextblueBold">Next&nbsp;<? echo $num;?></a>&nbsp;
        </span>
    <?php } ?>               
<?php
    } ?>                       
</div>


Comments