pagiLinks() Pagination Function

By Typo on Jun 09, 2011

Screenshots

For the sake of the screenshot, I made the background light grey, this is not included in the default styling.

See below for all the functions documentation.

/**
* @title Typos pagiLinks() Pagination Function (www.typo-it.com).
* @param (int) $curPage - This is the page that is currently loaded.
* @param (int) $totalResults - The total amount of results.
* @param (int) $maxResults - The maximum amount of results per page.
* @param (mixed) $urlQuery -  Any URL Queries that need to be appended to the pagination links.
*   Set to false for no queries or leave it out (ommit it) to use all existing queries.
* @return (string) A series of pagination links.
*
* Syntax Example Using All Existing Url Queries:
*   echo pagiLinks(7, 230, 10);
* Syntax Example Using Your Own Url Queries:
*   echo pagiLinks(7, 230, 10, 'sky=blue&snow=cold');
* Syntax Example Using No Additional Url Queries:
*   echo pagiLinks(7, 230, 10, false);
*
* Returned links example: 
*   Previous Page   1 ... 4 5 6 '7' 8 9 10 ... 23   Next Page
*
* Notes(1): 
*   The current page is styled to be bold and slightly larger using the provided the css, it
*       won't actually have quotes like above.  
* Notes(2): 
*   The url query string ($urlQuery) can look like 'sky=blue&snow=cold' or '&sky=blue&snow=cold' 
*   or '?sky=blue&snow=cold'.  If you do not include a URL query, the function will assume it
*   should pull the queries using $_SERVER, if you set $urlQuery to false, no additional url 
*   queries will be appended.
* Notes(3):
*   The included styling will spread accross the whole div you echo pagiLinks() in.  It will split 
*   it into thirds, in the first 1/3 will be the Previous Page link (when applicable), then, in the 
*   second 1/3, always centered is the numbered links and finally is the Next Page link
*   (if applicable) in the third 1/3.
* Notes(4):
*       The current page ($curPage) can be accessed the normal way using $_GET['curpage'].  I decided 
*       not to have the script grab this automatically for the sake of versatility.
* 
* Default CSS:              
*   .pagination { padding:0px 5px; width:100%; }
*   .pagiprevious { width:33%; text-align:left; float:left; }
*   .pagipages { text-align:center; }
*   .paginext { width:33%; text-align:right; float:right; }
*   .pagimiddle a { margin:0px 5px }
*   .pagifirst a { margin:0px 5px 0px 0px }
*   .pagilast a { margin:0px 0px 0px 5px }
*   .pagiselected { font-weight:bolder; font-size:110%; color:#505050; }
*/
function pagiLinks($curPage, $totalResults, $maxResults, $urlQuery = true) {
    $urlQuery = ($urlQuery === true) ? $_SERVER['QUERY_STRING'] : $urlQuery;    
    if (!empty($urlQuery)) {
        $urlQuery = (strpos($urlQuery, '?') === 0) ? substr($urlQuery, 1) : $urlQuery;
        parse_str($urlQuery,$urlQuery);
        $urlQuery = '&' . http_build_query(array_diff_key($urlQuery, array('curpage' => '')));
    }
    if ($totalResults > $maxResults) {
        $pagiLink = '<div class="pagination"><span class="pagiprevious">';
        $totalPages = ceil($totalResults / $maxResults);
        $pagiLink .= ($curPage - 1 > 0) ? "<a href=\"" . $_SERVER['PHP_SELF'] . '?curpage=' . ($curPage - 1) . $urlQuery . '">Previous Page</a></span>' : '&nbsp</span>';
        $pagiLink .= ($curPage + 1 <= $totalPages) ? "<span class=\"paginext\"><a href=\"" . $_SERVER['PHP_SELF'] . '?curpage=' . ($curPage + 1) . $urlQuery . '">Next Page</a></span>' : '<span class="paginext">&nbsp</span>';
        $selected = ($curPage == 1) ? 'class="pagiselected"' : '';
        $pagiLink .= "<span class=\"pagipages\"><span class=\"pagifirst\"><a $selected href=\"" . $_SERVER['PHP_SELF'] . "?curpage=1$urlQuery \">1</a>...</span><span class=\"pagimiddle\">";
        $i = ($curPage - 3 >= 2) ? $curPage - 3 : 2;
        while ($i < $totalPages) {
            $selected = ($curPage == $i) ? 'class="pagiselected"' : '';
            if ($i <= $curPage + 3) { $pagiLink .= "<a $selected href=\"" . $_SERVER['PHP_SELF'] . "?curpage=$i" . $urlQuery . " \">$i</a>"; }
            $i = $i + 1;
        }
        $selected = ($curPage == $totalPages) ? 'class="pagiselected"' : '';
        $pagiLink .= "</span><span class=\"pagilast\">...<a $selected href=\"" . $_SERVER['PHP_SELF'] . "?curpage=$totalPages" . $urlQuery . " \">$totalPages</a></span></span></div>";
        return "$pagiLink";
    }
}

Comments

Sign in to comment.
zenx   -  Jan 13, 2013

Thanks, using this in one project

 Respond  
F*U*R*B*Y*   -  Jun 20, 2011

omg keyboard died :(

 Respond  
F*U*R*B*Y*   -  Jun 20, 2011

Yeah thats good :)

awesome, i should attempt to fix my computer and get back into PHP, but its too much effort....

I got a new job, as a fulltime employee, and on a salary, and instead of doing the 40 hours a week, i've been doing 60 hours weeks to get the ropes

 Respond  
Typo   -  Jun 16, 2011

@Furby Lol, sorry for the late reply, been busy lately with customers projects.

I plan on becoming active again here now though and hopefully more so with PHP too.

Nice to see you!

 Respond  
F*U*R*B*Y*   -  Jun 12, 2011

Napa... Typo.... Wow you guys are still here??? Thought you guys moved on.... :)

 Respond  
napa182   -  Jun 09, 2011

nice work Typo
+Like

 Respond  
Typo   -  Jun 09, 2011

Feel free to let me know what you think. I know the styling is basic but I thought I would leave the fancy styling up to the individual.

Thanks for looking,

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.