1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Page wise display of data

Discussion in 'PHP' started by coderzone, Apr 2, 2006.

  1. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    To display the data by splitting them in pages is very important if you have chances of more records to be displayed.
    PHP:
    function page_records()

        global 
    $page,$pagecount,$perpage;
        
    $arr $_GET;
        if (
    is_array($arr))
        {
            foreach(
    $arr AS $_arrykey => $_arryval)
            {
                
    // Remove the page and perpage from the query string
                
    if($_arrykey == "page" OR $_arrykey == "perpage")
                    continue;
                
    $newarr["$_arrykey"] = $arr["$_arrykey"];
            }
        }
        
    $PREV_QUERY_STRING "?";
        if(isset(
    $newarr))
        {
            foreach(
    $newarr AS $_arrykey => $_arryval)
                
    $PREV_QUERY_STRING .= "$_arrykey=$_arryval&";
        }
        
    // output the page table.
        
    if($pagecount 1)
        {
            echo 
    "
            <table align=\"center\">
            <tr>"
    ;
            
    $curpage 1;
            while (
    $curpage != $page)
                
    $curpage++;

            if(
    $curpage <> 1)
            {
                
    $PHP_SELF $_SERVER["PHP_SELF"];
                
    $QUERY_STRING $PREV_QUERY_STRING."page=1&perpage=$perpage";
                echo
    "<td class=\"smallfont\">[ <a href=\"$PHP_SELF$QUERY_STRING\">&laquo;&laquo;</a> ]</td>";
            }
            
            for (
    $i $curpage-4;$i $curpage;$i++)
            {
                if(
    $i<1)
                    continue;
                
    $PHP_SELF $_SERVER["PHP_SELF"];
                
    $QUERY_STRING $PREV_QUERY_STRING."page=$i&perpage=$perpage";
                echo
    "<td class=\"smallfont\">[ <a href=\"$PHP_SELF$QUERY_STRING\">$i</a> ]</td>";
            }
    //end for

            
    echo "<td style=\"color:silver;\">[ $i ]</td>";

            for (
    $i=$curpage+1;$i <= $pagecount AND $i<=($curpage 9);$i++)
            {
                
    $PHP_SELF $_SERVER["PHP_SELF"];
                
    $QUERY_STRING $PREV_QUERY_STRING."page=$i&perpage=$perpage";
                echo
    "<td class=\"smallfont\">[ <a href=\"$PHP_SELF$QUERY_STRING\">$i</a> ]</td>"    ;
            }
    //end for
            
            
    if($curpage <> $pagecount)
            {
                
    $PHP_SELF $_SERVER["PHP_SELF"];
                
    $QUERY_STRING $PREV_QUERY_STRING."page=$pagecount&perpage=$perpage";
                echo
    "<td class=\"smallfont\">[ <a href=\"$PHP_SELF$QUERY_STRING\">&raquo;&raquo;</a> ]</td>";
            }
            
            echo
    "</tr></table>";
            echo
    "<br>";
        }
    }
    //end function 
    The above one uses smallfont CSS class which is defined as below
    Code:
    .smallfont
    {
        FONT: 8pt tahoma, verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif
    }
    
    Now all you need to be doing is on any page define the global variable values. You can get them through cookies if you have some preferences where user can select the no of records to be shown per page like Yahoo/GMail does.
    PHP:
    $page = isset($page)?$page:0;  //Page no we are on
    $perpage = isset($perpage)?$perpage:0;  //How much you need to show on one page
    $pagecount ceil($total_nr/$perpage);  // Total no of pages. 
    //$total_nr is the total no of records in the database for the current query.
    page_records();  // Call the function to display the page no.
     

Share This Page