Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Page wise display of data (http://www.go4expert.com/articles/page-wise-display-data-t675/)

coderzone 2Apr2006 14:23

Page wise display of data
 
To display the data by splitting them in pages is very important if you have chances of more records to be displayed.
PHP Code:

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: CSS

.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 Code:

$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. 



All times are GMT +5.5. The time now is 21:46.