Welcome Guest, Not a member yet? Register   Sign In
How to loop pagination links using for loop instead of using foreach loop
#5

(This post was last modified: 06-08-2022, 09:43 PM by venance.)

Thank you very much @webdeveloper  and @includebeer !.
Since I get trouble about extending Pager class,  I have decided to add some methods to straight to the system with namespace: CodeIgniter\Pager in codeigniter4/framework/system/Pager/Pager.php.
    So I can get the page number only without full url.
If is any way to extend pager class I would appreciate.

Pager.php
PHP Code:
public function getNextPageNumber(string $group 'default')
{
      $this->ensureGroup($group);

       $last $this->getLastPage($group);
       $curr $this->getCurrentPage($group);
       $page null;

       if (! empty($last) && ! empty($curr) && $last === $curr)
              return null;

       if ($last $curr)
              $page $curr 1;

       return $page;
}

public function 
getPreviousPageNumber(string $group 'default')
{
         $this->ensureGroup($group);

         $first $this->getFirstPage($group);
         $curr  $this->getCurrentPage($group);
         $page  0;

          if (! empty($first) && ! empty($curr) && $first === $curr)
                 return null;

           if ($first $curr)
                  $page $curr 1;

            return $page;
}

public function 
getCurrentPageNumber(string $group 'default')
{
        $this->ensureGroup($group);
        return (int) $this->getCurrentPage($group);


Here its's my custom pagination working.

PHP Code:
<?php 
        $pager
->setSurroundCount(2); 
        
$link        $pager->links(); 
        
$endGroup    1;
        
$middleGroup 2;
        
$dots        false;
        
$pageCount  $pager->getPageCount();
        
$currentPage $pager->getCurrentPageNumber();
?>

    <div class="pagination-container" aria-label="<?= lang('Pager.pageNavigation'?>">
        <?php if($pager->getPageCount() > 1): ?>
            <div class="paginate-info"> Showing {first_item_serial_page_number} to {last_item_serial_page_number} Of {getTotalresults}</div>
        <?php endif; ?>
        <ul class="pagination">
            <?php if ($pager->getPreviousPageNumber() !== null) : ?>
                <li class="page-item disabled">
                    <a href="<?= $pager->getPreviousPage() ?>"  data-page="<?= $pager->getPreviousPageNumber() ?>">
                        <span aria-hidden="true">Prev</span>
                    </a>
                </li>
            <?php else:?>
                <?php if($pager->getPageCount() > 1): ?>
                    <li class="disabled">Prev</li>
                <?php endif; ?>
            <?php endif ?>

            <?php for($i 0$i count($pager->links()); $i++) : ?>
                <?php if($i == $currentPage): ?>
                    <li <?= $link[$i]['active'] ? 'class="active"' '' ?>>
                        <a href="<?= $link[$i]['uri'?>" data-page="<?= $link[$i]['title'?>"><?= $link[$i]['title'?></a>
                    </li>
                    <?php $dots true?>
                 <?php else: ?>
                    <?php if($i <= $endGroup || ($currentPage && $i >= $currentPage $middleGroup && $i <= $currentPage $middleGroup) || $i $pageCount $endGroup): ?>
                        <li <?= $link[$i]['active'] ? 'class="active"' '' ?>>
                            <a href="<?= $link[$i]['uri'?>" data-page="<?= $link[$i]['title'?>"><?= $link[$i]['title'?></a>
                        </li>
                        <?php $dots true?>
                    <?php elseif($dots): ?>
                        <li>. . .</li>
                        <?php $dots false?>
                     <?php endif; ?>
                 <?php endif; ?>
            <?php endfor ?>


            <?php if ($pager->getNextPageNumber() !== null) : ?>
                <li>
                    <a href="<?= $pager->getNextPage() ?>" aria-label="<?= lang('Pager.next'?>" data-page="<?= $pager->getNextPageNumber() ?>">
                        <span aria-hidden="true"><?= lang('Pager.next'?></span>
                    </a>
                </li>
            <?php else: ?>
                <?php if($pager->getPageCount() > 1): ?>
                    <li class="disabled"><?= lang('Pager.next'?></li>
                <?php endif; ?>
            <?php endif ?>
        </ul>
    </div> 

I would like to hear from you about what I did, Also to improve the best pagination  that looks like this : prev 1 2 3 4 5 . . . 10 20 Next  to the final results.
Reply


Messages In This Thread
RE: How to loop pagination links using for loop instead of using foreach loop - by venance - 06-08-2022, 09:30 PM



Theme © iAndrew 2016 - Forum software by © MyBB