Welcome Guest, Not a member yet? Register   Sign In
Invalid argument supplied for foreach() Pagination
#5
Thumbs Up 
(This post was last modified: 08-07-2020, 04:19 AM by eleumas.)

There is some error in your code in bs_full.php or something that doesn't work in my project because i have replace your code with the original code il CodeIgniter documentation and pagination work fine.

So, this is my code for pagination. I use UIkit framework and not Bootstrap. Change the HTML classes like you need.

/app/Config/Pager.php
PHP Code:
<?php namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
Pager extends BaseConfig
{
    
/*
    |--------------------------------------------------------------------------
    | Templates
    |--------------------------------------------------------------------------
    |
    | Pagination links are rendered out using views to configure their
    | appearance. This array contains aliases and the view names to
    | use when rendering the links.
    |
    | Within each view, the Pager object will be available as $pager,
    | and the desired group as $pagerGroup;
    |
    */
    
public $templates = [
        
'default_full'   => 'CodeIgniter\Pager\Views\default_full',
        
'default_simple' => 'CodeIgniter\Pager\Views\default_simple',
        
'default_head'   => 'CodeIgniter\Pager\Views\default_head',
        
'uk_full'        => 'App\Views\pagers\uk_full',
        
'uk_simple'      => 'App\Views\pagers\uk_simple',
    ];
    
    
/*
    |--------------------------------------------------------------------------
    | Items Per Page
    |--------------------------------------------------------------------------
    |
    | The default number of results shown in a single page.
    |
    */
    
public $perPage 20;


/app/Views/pagers/uk_full.php
PHP Code:
<?php $pager->setSurroundCount(2); ?>

<nav aria-label="pagination">
  <ul class="uk-pagination uk-flex-center">
    <?php if ($pager->hasPreviousPage()) : ?>
      <li>
        <a href="<?= $pager->getFirst() ?>" aria-label="<?= lang('Pager.first'?>">
          <span aria-hidden="true"><?= lang('Pager.first'?></span>
        </a>
      </li>
      <li>
        <a href="<?= $pager->getPreviousPage() ?>" aria-label="<?= lang('Pager.previous'?>">
          <span aria-hidden="true"><?= lang('Pager.previous'?></span>
        </a>
      </li>
    <?php endif ?>
    
    <?php foreach ($pager->links() as $link) : ?>
      <li <?= $link['active'] ? 'class="uk-active"' '' ?>>
        <a href="<?= $link['uri'?>">
          <?= $link['title'?>
        </a>
      </li>
    <?php endforeach ?>
    
    <?php if ($pager->hasNextPage()) : ?>
      <li>
        <a href="<?= $pager->getNextPage() ?>" aria-label="<?= lang('Pager.next'?>">
          <span aria-hidden="true"><?= lang('Pager.next'?></span>
        </a>
      </li>
      <li>
        <a href="<?= $pager->getLast() ?>" aria-label="<?= lang('Pager.last'?>">
          <span aria-hidden="true"><?= lang('Pager.last'?></span>
        </a>
      </li>
    <?php endif ?>
  </ul>
</nav> 

/app/Views/pagers/uk_simple.php
PHP Code:
<?php $pager->setSurroundCount(2); ?>

<nav aria-label="pagination">
  <ul class="uk-pagination uk-flex-center">
    <?php if ($pager->hasPreviousPage()) : ?>
      <li>
        <a href="<?= $pager->getFirst() ?>" aria-label="<?= lang('Pager.first'?>">
          <span aria-hidden="true"><?= lang('Pager.first'?></span>
        </a>
      </li>
      <li>
        <a href="<?= $pager->getPreviousPage() ?>" aria-label="<?= lang('Pager.previous'?>">
          <span aria-hidden="true"><?= lang('Pager.previous'?></span>
        </a>
      </li>
    <?php endif ?>
    <?php if ($pager->hasNextPage()) : ?>
      <li>
        <a href="<?= $pager->getNextPage() ?>" aria-label="<?= lang('Pager.next'?>">
          <span aria-hidden="true"><?= lang('Pager.next'?></span>
        </a>
      </li>
      <li>
        <a href="<?= $pager->getLast() ?>" aria-label="<?= lang('Pager.last'?>">
          <span aria-hidden="true"><?= lang('Pager.last'?></span>
        </a>
      </li>
    <?php endif ?>
  </ul>
</nav> 

In your view (for example under the items table):
PHP Code:
  <?php
  
if ( ! empty($pager))
  {
    //echo $pager->simpleLinks('group1', 'uk_simple'); 
    echo $pager->links('group1''uk_full');
  }
  ?>

/app/Controllers/NAME_OF_YOUR_CONTROLLER.php
PHP Code:
  public function index()
  {
    $pager = \Config\Services::pager();
    $model = new \App\Models\ArticleModel();

    $data = [
      'articles'   => $model->orderBy('id''DESC')->paginate(5'group1'),
      'pager'      => $model->pager,
    ];

    echo view('articles/index'$data);
  

Enjoy!   Cool
Reply


Messages In This Thread
RE: Pagination CodeIgniter 4 - by eleumas - 08-07-2020, 03:55 AM



Theme © iAndrew 2016 - Forum software by © MyBB