Results 1 to 6 of 6

Thread: [RESOLVED] Pagination Function

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] Pagination Function

    I'm basically working on a pagination function. Here's what I have so far:

    PHP Code:
    <?php

    function printPageIndexes($currentPage$totalPages$theURL$pageGET$printExtras="")
    {

    if (
    $printExtras!="")
    {
      if (
    $currentPage!=1)
        {
        
    $toScreen $toScreen ' <a href="' $theURL $pageGET '"=1">&lt;&lt;</a> ';
        
    $toScreen $toScreen ' <a href="' $theURL $pageGET "=" . ($currentPage-1) . '">&lt;</a> ';
        }
    }

      if (
    $totalPages >=10)
        {
          
            if (
    $currentPage<=|| $currentPage>=($totalPages-5))
            {
              while(
    $i <= $totalPages)
              {

            if (
    $i!=$currentPage)
                {
                      
    // ******************************
                      
    if ($currentPage==|| $currentPage==$totalPages)
                        {
                          if (
    $i <=|| $i >=($totalPages-5))
                            {
                            
    $toScreen $toScreen ' <a href="' $theURL $pageGET "=" $i '">' $i '</a> ';
                            }
                            else
                            {
                            if (!
    $dotsPrinter)
                              {
                              
    $dotsPrinter=true;
                              
    $toScreen $toScreen '[...]';
                                }
                            }
                            
                        
    // ******************************
                        
    }
                        else
                        {
                      
    $toScreen $toScreen ' <a href="' $theURL $pageGET "=" $i '">' $i '</a> ';
                  }
                    }
                else
                {
                    
    $toScreen $toScreen $i;
                }
            
    $i++;
                }
            }
            
        }
        else
        {
          while(
    $i <= $totalPages)
            {
          if (
    $i!=$currentPage)
              {
                  
    $toScreen $toScreen ' <a href="' $theURL $pageGET "=" $i '">' $i '</a> ';
              }
              else
              {
                  
    $toScreen $toScreen $i;
              }
          
    $i++;
        }
        }

    if (
    $printExtras!="")
    {
      if (
    $currentPage!=$totalPages)
        {
        
    $toScreen $toScreen ' <a href="' $theURL $pageGET "=" . ($currentPage+1) . '">&gt;</a> ';
        
    $toScreen $toScreen ' <a href="' $theURL $pageGET "=" $totalPages '">&gt;&gt;</a> ';
        }
    }
        
        return 
    $toScreen;
        
    }

    It's not finished yet as you can tell.

    I basically want to get results like this:
    1 2 3 4 5 [...] 56 57 58 59 60 61
    or
    1 2 3 4 5 [...] 26 27 28 29 30 31 32 33 34 35 36 [...] 56 57 58 59 60 61
    or
    1 2 3 4 5 6 7 8 9 10 11 12 [...] 56 57 58 59 60 61
    or
    1 [...] 3 4 5 6 7 8 9 10 11 12 13 [...] 56 57 58 59 60 61

    Where the bolded number is the current page.

    It's basically show 5 pages surrounding the current page, unless it's within 5 of the first page, or the total pages.

    My problem is that I can't figure out how to do it.

    I'm going to keep trying to work it out, but any help is appreciated .

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Pagination Function

    Figured it out .
    PHP Code:
    <?php
    /*
    Author: Steven Lawler (Slyke The Phoxenix)
    Email: [email protected]
    Website: http://www.stevenlawler.name

    Useage:
    Leave credits in here, but you are free to modify the code as you wish.
    Just include this file in your code and call the 'printPageIndexes' function.
    */

    function printPageIndexes($currentPage$totalPages$theURL$pageGET$controlIndexLimit=5$printExtras=""$showAllText="")
    {

    // currentPage = The current page you are on.
    // totalPages = Total amount of pages to be displayed.
    // theURL = The URL that you have for your page.
    // pageGET = The page variable you use in PHP to pass pages to your script from requests. You will need to place a ? or & where necessary.
    // controlIndexLimit = The amount of indexes that will be shown around your current page. EG if you are on page 35, and set this to 5, it would show 30 31 32 33 34 [35] 36 37 38 39 40.
    // printExtras = show <<, <, > & >>
    // showAllText = Show the "All Pages" hyperlink, place the data into here that you use in your script to show all items (no pagination).

    if ($printExtras!="")
    {
      if (
    $currentPage!=1)
        {
        
    $toScreen $toScreen ' <a href="' $theURL $pageGET '=1">&lt;&lt;</a> ';
        
    $toScreen $toScreen ' <a href="' $theURL $pageGET "=" . ($currentPage-1) . '">&lt;</a> ';
        }
    }

      if (
    $totalPages >=10)
        {
            while(
    $i <= $totalPages)
            {

          if (
    $i!=$currentPage)
              {
                    
                if (
    $i<=$controlIndexLimit || $i >=($totalPages-$controlIndexLimit))
            {
                      
    $toScreen $toScreen ' <a href="' $theURL $pageGET "=" $i '">' $i '</a> ';
                  }
                    elseif (
    $i>=($currentPage-$controlIndexLimit) && $i<=($currentPage+$controlIndexLimit))
                    {
                      
    $toScreen $toScreen ' <a href="' $theURL $pageGET "=" $i '">' $i '</a> ';
                    }
                    elseif (
    $i<=($currentPage-$controlIndexLimit))
                    {
                      if (!
    $showDotsLower)
                        {
                        
    $showDotsLower=true;
                            
    $toScreen $toScreen '[...]';
                        }
                    }
                    elseif (
    $i>=($currentPage+$controlIndexLimit))
                    {
                      if (!
    $showDotsUpper)
                        {
                        
    $showDotsUpper=true;
                            
    $toScreen $toScreen '[...]';
                        }
                    }
                }
              else
              {
                  
    $toScreen $toScreen $i;
              }
          
    $i++;
            }
            
        }
        else
        {
          while(
    $i <= $totalPages)
            {
          if (
    $i!=$currentPage)
              {
                  
    $toScreen $toScreen ' <a href="' $theURL $pageGET "=" $i '">' $i '</a> ';
              }
              else
              {
                  
    $toScreen $toScreen $i;
              }
          
    $i++;
        }
        }

    if (
    $printExtras!="")
    {
      if (
    $currentPage!=$totalPages)
        {
        
    $toScreen $toScreen ' <a href="' $theURL $pageGET "=" . ($currentPage+1) . '">&gt;</a> ';
        
    $toScreen $toScreen ' <a href="' $theURL $pageGET "=" $totalPages '">&gt;&gt;</a> ';
        }
    }

    if (
    $showAllText!="")
    {
      
    $toScreen $toScreen ' | <a href="' $theURL $pageGET "=" $showAllText '">All Pages</a> ';
    }

        return 
    $toScreen;
        
    }
    Last edited by Slyke; May 9th, 2009 at 01:12 PM.

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] Pagination Function

    I was writing my own function to help with this, but I found a function that does it well already.
    PHP Code:
    //function to return the pagination string
    function getPaginationString($page 1$totalitems$limit 15$adjacents 5$targetpage "/"$pagestring "?page=")
    {        
        
    //defaults
        
    if(!$adjacents$adjacents 1;
        if(!
    $limit$limit 15;
        if(!
    $page$page 1;
        if(!
    $targetpage$targetpage "/";
        
        
    //other vars
        
    $prev $page 1;                                    //previous page is page - 1
        
    $next $page 1;                                    //next page is page + 1
        
    $lastpage ceil($totalitems $limit);                //lastpage is = total items / items per page, rounded up.
        
    $lpm1 $lastpage 1;                                //last page minus 1
        
        /* 
            Now we apply our rules and draw the pagination object. 
            We're actually saving the code to a variable in case we want to draw it more than once.
        */
        
    $pagination "";
        if(
    $lastpage 1)
        {    
            
    $pagination .= "<div class=\"pagination\"";
            if(
    $margin || $padding)
            {
                
    $pagination .= " style=\"";
                if(
    $margin)
                    
    $pagination .= "margin: $margin;";
                if(
    $padding)
                    
    $pagination .= "padding: $padding;";
                
    $pagination .= "\"";
            }
            
    $pagination .= ">";

            
    //previous button
            
    if ($page 1
                
    $pagination .= "<a href=\"$targetpage$pagestring$prev\">« prev</a>";
            else
                
    $pagination .= "<span class=\"disabled\">« prev</span>";    
            
            
    //pages    
            
    if ($lastpage + ($adjacents 2))    //not enough pages to bother breaking it up
            
    {    
                for (
    $counter 1$counter <= $lastpage$counter++)
                {
                    if (
    $counter == $page)
                        
    $pagination .= "<span class=\"current\">$counter</span>";
                    else
                        
    $pagination .= "<a href=\"" $targetpage $pagestring $counter "\">$counter</a>";                    
                }
            }
            elseif(
    $lastpage >= + ($adjacents 2))    //enough pages to hide some
            
    {
                
    //close to beginning; only hide later pages
                
    if($page + ($adjacents 3))        
                {
                    for (
    $counter 1$counter + ($adjacents 2); $counter++)
                    {
                        if (
    $counter == $page)
                            
    $pagination .= "<span class=\"current\">$counter</span>";
                        else
                            
    $pagination .= "<a href=\"" $targetpage $pagestring $counter "\">$counter</a>";                    
                    }
                    
    $pagination .= "<span class=\"elipses\">...</span>";
                    
    $pagination .= "<a href=\"" $targetpage $pagestring $lpm1 "\">$lpm1</a>";
                    
    $pagination .= "<a href=\"" $targetpage $pagestring $lastpage "\">$lastpage</a>";        
                }
                
    //in middle; hide some front and some back
                
    elseif($lastpage - ($adjacents 2) > $page && $page > ($adjacents 2))
                {
                    
    $pagination .= "<a href=\"" $targetpage $pagestring "1\">1</a>";
                    
    $pagination .= "<a href=\"" $targetpage $pagestring "2\">2</a>";
                    
    $pagination .= "<span class=\"elipses\">...</span>";
                    for (
    $counter $page $adjacents$counter <= $page $adjacents$counter++)
                    {
                        if (
    $counter == $page)
                            
    $pagination .= "<span class=\"current\">$counter</span>";
                        else
                            
    $pagination .= "<a href=\"" $targetpage $pagestring $counter "\">$counter</a>";                    
                    }
                    
    $pagination .= "...";
                    
    $pagination .= "<a href=\"" $targetpage $pagestring $lpm1 "\">$lpm1</a>";
                    
    $pagination .= "<a href=\"" $targetpage $pagestring $lastpage "\">$lastpage</a>";        
                }
                
    //close to end; only hide early pages
                
    else
                {
                    
    $pagination .= "<a href=\"" $targetpage $pagestring "1\">1</a>";
                    
    $pagination .= "<a href=\"" $targetpage $pagestring "2\">2</a>";
                    
    $pagination .= "<span class=\"elipses\">...</span>";
                    for (
    $counter $lastpage - (+ ($adjacents 3)); $counter <= $lastpage$counter++)
                    {
                        if (
    $counter == $page)
                            
    $pagination .= "<span class=\"current\">$counter</span>";
                        else
                            
    $pagination .= "<a href=\"" $targetpage $pagestring $counter "\">$counter</a>";                    
                    }
                }
            }
            
            
    //next button
            
    if ($page $counter 1
                
    $pagination .= "<a href=\"" $targetpage $pagestring $next "\">next »</a>";
            else
                
    $pagination .= "<span class=\"disabled\">next »</span>";
            
    $pagination .= "</div>\n";
        }
        
        return 
    $pagination;


    courtesy these guys.

    the only real difference is that this function requires the number of items and the number of items you'd like per page, rather than the total number of pages.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: [RESOLVED] Pagination Function

    There is also a link to one in the PHP FAQ.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: [RESOLVED] Pagination Function

    Slyke do you have a working example for this fucntion?

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: [RESOLVED] Pagination Function

    I sure do, I'll explain it a little for people who find it hard to follow my code:

    The software itself is rather huge, but I've posted up a modified snippet of it, where it pulls the data out of the database and paginates it. The example I've provided actually performs the SQL Query twice (One time to get the amount of results, and the second time to get the actual data), but you can use "$numRows = count($currentPageRows);" if you can figure out a way to do so.

    Basically, you just need to add this to your SQL:
    PHP Code:
    if ($currentPage!="all")
    {
      if (
    $currentPage 1)
        {
          
    $currentPage 1;
        } 

        
    $numRowsQueryResult mysql_query($MySQL_Query ";"$dbConn);
        
        
    $numRows mysql_num_rows($numRowsQueryResult);
        
        
    $MySQL_Query .= "LIMIT " . (($currentPage*$numberOfResultsPerPage)-$numberOfResultsPerPage) . ", " $numberOfResultsPerPage;
        
    }


    $currentPageRows mysql_query($MySQL_Query ";"$dbConn); 
    You would get the current page number and place it into "$currentPage" from $_GET.


    Here's what calls the pagination:

    PHP Code:
      if ($currentPage!="all" && ceil($numRows/$numberOfResultsPerPage) >= 1)
      {
        echo 
    "<center>Page: ";
            
            
    $totalPages=ceil($numRows/$numberOfResultsPerPage);
            
            echo 
    printPageIndexes($currentPage$totalPages$myCurrentURL"&page"5true"all");
      } 
    Last edited by Slyke; May 11th, 2009 at 03:32 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width