Home / PHP Pagination Function

PHP Pagination Function

If you are looking for an easy and effective solution for PHP pagination then you are in the right place now.

You just need to provide 2 parameters to call this function to show the awesome pagination.

Pagination(['LastPage' => 25, 'PageNumber' => 8]); // Call the pagination function

function Pagination_($Data = null){
    // LastPage -required // LastPage mean the last number of pagination
    // PageNumber -required // PageNumber mean the current number on that pagination
    if($Data){
        extract($Data);
    }
    ob_start();

    $PrePage  = ($PageNumber > 2)?($PageNumber - 1):null;
    $NextPage = ($PageNumber < $LastPage)?($PageNumber + 1):null;
    
    if($LastPage <= 9){
        $partFirst  = (object)['start' => 1, 'limit' => $LastPage];
    }else{

        if($PageNumber <= 4){
            $partFirst  = (object)['start'=>1, 'limit'=>6];
            $partLast   = (object)['start'=> $LastPage-1 , 'limit'=> $LastPage ];

            $dotFirst   = false;
            $dotLast    = true;
        }elseif( $PageNumber >= ($LastPage - 4)){
            $partFirst  = (object)['start'=> 1, 'limit'=> 2 ];
            $partLast   = (object)['start'=> $LastPage-5 , 'limit'=> $LastPage ];

            $dotFirst   = true;
            $dotLast    = false;
        }else{
            $partFirst  = (object)['start'=> 1, 'limit'=> 2];
            $partMiddle = (object)['start'=> $PageNumber - 1, 'limit'=> $PageNumber+1];
            $partLast   = (object)['start'=> $LastPage-1 , 'limit'=> $LastPage ];

            $dotFirst   = true;
            $dotLast    = true;
        }
    }
    

    ?>
        <li
            class='page-item force-drop SetPage <?php echo (!$PrePage)?'disabled':'';?>'
            data-page='<?php echo ($PrePage)?:''; ?>'>
            <a class='page-link' href='#'><?php _e('Prev', 'prism'); ?></a>
        </li>

        <?php if($partFirst){ for( $navItem = $partFirst->start; $navItem <= $partFirst->limit ; $navItem++ ){ ?>
            <li
                class='page-item page-number SetPage <?php echo ($PageNumber == $navItem)?'active':''; ?>'
                PageNumber='<?php echo $navItem; ?>'
                data-page='<?php echo $navItem; ?>'>
                <a class='page-link' href='#'>
                    <?php echo $navItem; ?>
                </a>
            </li>
        <?php } } ?>
    
        <?php if($dotFirst){ ?>
            <li class='page-item page-next item_bulk'><a class='page-link' href='#'>...</a></li>
        <?php } ?>

        <?php if($partMiddle){ for( $navItem = $partMiddle->start; $navItem <= $partMiddle->limit ; $navItem++ ){ ?>
            <li
                class='page-item page-number SetPage <?php echo ($PageNumber == $navItem)?'active':''; ?>'
                PageNumber='<?php echo $navItem; ?>'
                data-page='<?php echo $navItem; ?>'>
                <a class='page-link' href='#'>
                    <?php echo $navItem; ?>
                </a>
            </li>
        <?php } } ?>
    

        <?php if($dotLast){ ?>
            <li class='page-item page-next item_bulk'><a class='page-link' href='#'>...</a></li>
        <?php } ?>

        <?php if($partLast){ for( $navItem = $partLast->start; $navItem <= $partLast->limit ; $navItem++ ){ ?>
            <li
                class='page-item page-number SetPage <?php echo ($PageNumber == $navItem)?'active':''; ?>'
                PageNumber='<?php echo $navItem; ?>'
                data-page='<?php echo $navItem; ?>'>
                <a class='page-link' href='#'>
                    <?php echo $navItem; ?>
                </a>
            </li>
        <?php } } ?>

        <li
            class='page-item force-drop page-next SetPage  <?php echo (!$NextPage)?'disabled':'';?>'
            PageNumber='<?php echo $PageNumber;?>'
            data-page='<?php echo $NextPage;?>'>
            <a class='page-link' href='#'>
                <?php _e('Next', 'prism'); ?>
            </a>
        </li>
    <?php

    return ob_get_clean();

}

all rights reserved