pagination help

Discussion in 'PHP' started by dracula51, Jun 3, 2009.

  1. #1
    Hi, here's the code i used to add Pagination at my PHP page

    <?php
    
    include("./db_connect.php"); //database connection information
    
    $select = mysql_query("SELECT * FROM table_name");
    $num_rows = mysql_num_rows($select);
    
    $totalrecord=1000; // It declairs total row number of our table, value should be $num_rows but for example of huge data I manually added 1000
    $pagesize=20; // It declairs number of database row content show in per page. 20 means 20 item per page
    
    $noofpages=$totalrecord/$pagesize; // as I set $totalrecord=1000 & $pagesize=20...so there'll be total 500 pages
    
    if (!isset($_REQUEST['startdata']))
    $startdata=0;
    else
    $startdata=$_REQUEST['startdata'];
    $count=$startdata;
    
    
    /*
    PAGE CONTENT & MySQL DATA TABLE's RESULT...Like-
    example: $select = mysql_query("SELECT * FROM table_name ORDER BY some_order DESC limit ".$startdata.",".$pagesize."");
    */
    
    
    if($startdata<>0) 
    {
        $prev=$startdata-$pagesize;
        echo "<a href='home.php?startdata=".$prev."'><b>- Prev</b></a>&nbsp;&nbsp;";
    }
    
    for ($i=0;$i<$noofpages;$i++)
    {
        $pageno=$i+1; $j=($pagesize*$pageno)-$pagesize;
    
        if ($startdata==0 && $i==0) { echo "Page ". $pageno."</b> "; }
            else
                {
                    if($startdata == ($pagesize*($pageno))-$pagesize)
                        {
                            echo "". $pageno." ";
                        }
            else
                {
                    echo "<a href='home.php?startdata=".$j."'>". $pageno. "</a> ";
                }
                }
    }
    
    if($startdata+$pagesize<$totalrecord)
    {
        $next=$startdata+$pagesize;
        echo "&nbsp;&nbsp;<a href='home.php?startdata=".$next."'>Next - </a>";
    }
    
    
    ?>
    PHP:
    so the result is something like this:

    [​IMG]

    i mean it is showing every page from 1 to 500
    its kinda messy

    i want it to show something like this: Prev | 1 2 3 4 17 51 99 160 278 327 418 499 500 | Next
    well its just an example. It shows that, all 500 page are shown within 13 number/link by ignoring other page

    or maybe like this: Prev | 1 2 3 4 ...... 498 499 500 | Next
    it added shows first (& last) 4 pages & added .... at middle of them

    i wanted this just to save page space

    someone plz help me

    & plz dont suggest me to google something cause i searched there many times:(
     
    dracula51, Jun 3, 2009 IP
  2. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    atlantaazfinest, Jun 4, 2009 IP
  3. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    tnx. thats what i want
    i'll try it
     
    dracula51, Jun 4, 2009 IP