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

Hello everyone, I'm trying to loop link in my pagination template view, I want to be able to check various condition with the counter,
for example:
PHP Code:
<?php for($i 1$i <= count($pager->links()); $i++): ?>
       <li style="padding:5px"><?= $pager->links()['title'][$i?></li>
<?php endfor; ?>
But I want to be able to uset array items within $pager->links(), uri, title est. Like when It used with foreach like this
PHP Code:
<?php foreach ($pager->links() as $link) : ?>
     <li <?= $link['active'] ? 'class="active"' '' ?>><a href="<?= $link['uri'?>" ?>"><?= $link['title'?></a></li>
<?php endforeach ?>
How this can be done with for loop?
Thank you in advance!
Reply
#2

(This post was last modified: 06-08-2022, 08:29 AM by webdeveloper.)

I really don't know why you need this, but did you try it like below? If it's associative array, it should work.

PHP Code:
<?php for($i = 0; $i < count($pager->links()); $i++): ?>
       <li <?= $pager->links()[$i]['active'] ? 'class="active"' : '' ?>><?= $pager->links()[$i]['title'?></li>
<?php endfor; ?>
Reply
#3

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

Thank you @webdeveloper very much! It's working as expected, But I have a question about setting up the counter to begin with 1 instead of  0,
PHP Code:
<?php 
      $link 
$pager->links();
?>
<?php 
for($i 1$i count($pager->links()); $i++): ?>
<?php 
if($i >= ): ?>
        <li> <?=  $link[$i]['title'?></li>
<?php endif; ?>
<?php 
endfor; ?>
The purpose of doing this is to be able to create ci4 pagination with the look like this: Prev 1 2 3 4 5 . . . 10 20 Next. So I can be able to generate some logic's to meet this features.
I would like to hear any advice or alternatives of doing the same with ci4 pager.
Reply
#4

Why not just add the key to the foreach like this:
PHP Code:
<?php foreach ($pager->links() as $i => $link) : ?>
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#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
#6

getCurrentPageNumber()
This method returns the page number of the current page.

getPageCount()
This method returns total number of pages.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(This post was last modified: 06-09-2022, 01:33 AM by venance.)

(06-09-2022, 12:11 AM)InsiteFX Wrote: getCurrentPageNumber()
This method returns the page number of the current page.

getPageCount()
This method returns total number of pages.

Yes exactly, the getCurrentPageNumber() return null | int, thus why I have to check if is greater than 0 be sure.
This help me when I want to use data attribute like data-page="1".
But, which way I can extend Pager class and add those methods instead of adding to the core so incase the system upgraded either with composer update, the library will not be affected.

(06-09-2022, 12:11 AM)InsiteFX Wrote: getCurrentPageNumber()
This method returns the page number of the current page.

getPageCount()
This method returns total number of pages.

Yes exactly, the getCurrentPageNumber() return null | int, thus why I have to check if is greater than 0 be sure.
This help me when I want to use data attribute like data-page="1".
But, which way I can extend Pager class and add those methods instead of adding to the core so incase the system upgraded either with composer update, the library will not be affected.
Reply
#8

Then you must have something wrong in your code because I use both of those for displaying

Page 1 of 25

etc; And they work just fine.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

(This post was last modified: 06-10-2022, 04:23 AM by venance.)

(06-09-2022, 04:09 AM)InsiteFX Wrote: Then you must have something wrong in your code because I use both of those for displaying

Page 1 of 25

etc; And they work just fine.

No I didn't mean I get any error, the reason is to find the best way to extend pager class.
eg
PHP Code:
CustomPagination extends Pager
{
      // some extra methods here like
     public function getNextPageNumber(){}
     public function getPreviousPageNumber(){}
     public function getCurrentPageNumber(){}


How this can be done?
Also, Can you please help me to improve the logic's in my pagination view. I think it's not working as I expected for the first number of pages after dots and the last number of pages to be shown after dots eg prev 1 2 3  . . . 10 11 12 next.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB