php facebook pagination

PHP facebook pagination class

http://forum.developers.facebook.net/viewtopic.php?pid=88426

[ad code=4 align=center]

<?php

/**
 * Facebook style paginator
 *
 * @param page the page number (1-based)
 * @param total_items the total amount of items
 * @param limit the amount of items to show per page
 * @param ajax an array with each of clickrewrite[url|id|form]
 * @param url the url the links point to
 * @param query_string the string to be appended to url
 * @param item name the name of items shown in summary
 * @param position whether paginator is on top or bottom of page
 * @return pagination string to be placed in html code
 */
function get_pagination_string(
        $page = 1,
        $total_items,
        $limit = 8,
        $ajax='',
        $target_page = '',
        $page_string = '?page=',
        $summary_name = 'items',
        $placement = 'summary'
        )
{
    // DEFAULTS
    $adjacents = 5;

    // HTML
    $div_bar = '<div>';
    $display = '<div>Displaying %d-%d of %d %s.</div>';
    $ul = '<ul id="pag_nav_links">';
    $link = '<li><a href="%s%s%s">%s</a></li>';
    $current_link = '<li><a href="%s%s%s">%s</a></li>';
    $ajax_link = '<li><a href="#" clickrewriteurl="%s&page=%s" clickrewriteid="%s" clickrewriteform="%s">%s</a></li>';
    $current_ajax_link = '<li><a href="#" clickrewriteurl="%s&page=%s" clickrewriteid="%s" clickrewriteform="%s">%s</a></li>';
    $ul_close = '</ul>';
    $div_close = '</div>';
    $div_font = '<div style="font-size:11px;">%s</div>';

    // VARS
    if(substr($page_string, 0, 1) != '?')
        $page_string = '?' . $page_string;
    if(substr($page_string, strlen($page_string) - 5) != 'page=')
        $page_string .= '&page=';
    $prev = $page - 1;
    $next = $page + 1;
    $firstpage = 1;
    $lastpage = ceil($total_items / $limit);
    $fiop = ($limit * $page) - $limit + 1;
    $liop = min($limit * $page, $total_items);

    // DRAW PAGINATOR 

    // the footer paginator has no summary and the current page has a different gfx
    $type = ($placement == 'summary') ? 'summary' : 'footer';

    $pagination = sprintf($div_bar, $type);

    // Draw summary
    if($placement == 'summary')
        $pagination .= sprintf($display, $fiop, $liop, $total_items, $summary_name);

    if($lastpage > 1)
    {
        $pagination .= $ul;

        // First page selector
        if ($page > 2)
        {
            if(!empty($ajax))
            {
                $pagination .= sprintf(
                        $ajax_link,
                        $ajax['rewriteurl'],
                        $firstpage,
                        $ajax['rewriteid'],
                        $ajax['rewriteform'],
                        'First');
            }
            else
            {
                $pagination .= sprintf(
                        $link,
                        $target_page,
                        $page_string,
                        $firstpage,
                        'First');
            }
        }

        // Previous page selector
        if ($page > 1)
        {
            if(!empty($ajax))
            {
                $pagination .= sprintf(
                        $ajax_link,
                        $ajax['rewriteurl'],
                        $firstpage,
                        $ajax['rewriteid'],
                        $ajax['rewriteform'],
                        'Prev');
            }
            else
            {
                $pagination .= sprintf(
                        $link,
                        $target_page,
                        $page_string,
                        $firstpage,
                        'Prev');
            }
        }

        // Page selectors
        if ($page < 4)
        {
            for ($counter = 1; $counter <= min(5, $lastpage); $counter++)
            {
                if ($counter == $page)
                {
                    if(!empty($ajax))
                    {
                        $pagination .= sprintf(
                                $current_ajax_link,
                                $ajax['rewriteurl'],
                                $counter,
                                $ajax['rewriteid'],
                                $ajax['rewriteform'],
                                $counter);
                    }
                    else
                    {
                        $pagination .= sprintf(
                                $current_link,
                                $target_page,
                                $page_string,
                                $counter,
                                $counter);
                    }
                }
                else
                {
                    if(!empty($ajax))
                    {
                        $pagination .= sprintf(
                                $ajax_link,
                                $ajax['rewriteurl'],
                                $counter,
                                $ajax['rewriteid'],
                                $ajax['rewriteform'],
                                $counter);
                    }
                    else
                    {
                        $pagination .= sprintf(
                                $link,
                                $target_page,
                                $page_string,
                                $counter,
                                $counter);
                    }
                }
            }
        }
        elseif ($page > $lastpage - 3)
        {
            for($counter = $lastpage - min(5, $lastpage);
                    $counter <= $lastpage;
                    $counter++)
            {
                if ($counter == $page)
                {
                    if(!empty($ajax))
                    {
                        $pagination .= sprintf(
                                $current_ajax_link,
                                $ajax['rewriteurl'],
                                $counter,
                                $ajax['rewriteid'],
                                $ajax['rewriteform'],
                                $counter);
                    }
                    else
                    {
                        $pagination .= sprintf(
                                $current_link,
                                $target_page,
                                $page_string,
                                $counter,
                                $counter);
                    }
                }
                else
                {
                    if(!empty($ajax))
                    {
                        $pagination .= sprintf(
                                $ajax_link,
                                $ajax['rewriteurl'],
                                $counter,
                                $ajax['rewriteid'],
                                $ajax['rewriteform'],
                                $counter);
                    }
                    else
                    {
                        $pagination .= sprintf(
                                $link,
                                $target_page,
                                $page_string,
                                $counter,
                                $counter);
                    }
                }
            }
        }
        else
        {
            for($counter = $page - 2; $counter <= $page + 2; $counter++)
            {
                if ($counter == $page)
                {
                    if(!empty($ajax))
                    {
                        $pagination .= sprintf(
                                $current_ajax_link,
                                $ajax['rewriteurl'],
                                $counter,
                                $ajax['rewriteid'],
                                $ajax['rewriteform'],
                                $counter);
                    }
                    else
                    {
                        $pagination .= sprintf(
                                $current_link,
                                $target_page,
                                $page_string,
                                $counter,
                                $counter);
                    }
                }
                else
                {
                    if(!empty($ajax))
                    {
                        $pagination .= sprintf(
                                $ajax_link,
                                $ajax['rewriteurl'],
                                $counter,
                                $ajax['rewriteid'],
                                $ajax['rewriteform'],
                                $counter);
                    }
                    else
                    {
                        $pagination .= sprintf(
                                $link,
                                $target_page,
                                $page_string,
                                $counter,
                                $counter);
                    }
                }
            }
        }

        //next button
        if ($page < $lastpage)
        {

            if(!empty($ajax))
            {
                $pagination .= sprintf(
                        $ajax_link,
                        $ajax['rewriteurl'],
                        $next,
                        $ajax['rewriteid'],
                        $ajax['rewriteform'],
                        'Next');
            }
            else
            {
                $pagination .= sprintf(
                        $link,
                        $target_page,
                        $page_string,
                        $next,
                        'Next');
            }                       

        }

        //last button
        if ($page < $lastpage - 1)
        {
            if(!empty($ajax))
            {
                $pagination .= sprintf(
                        $ajax_link,
                        $ajax['rewriteurl'],
                        $lastpage,
                        $ajax['rewriteid'],
                        $ajax['rewriteform'],
                        'Last');
            }
            else
            {
                $pagination .= sprintf(
                        $link,
                        $target_page,
                        $page_string,
                        $lastpage,
                        'Last');
            }
        }
        $pagination .= $ul_close;
    }

    $pagination .= $div_close;
    $pagination = sprintf($div_font, $pagination);

    return $pagination;
}

Leave a comment