• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Invalid argument supplied for foreach() Pagination

#1
Hi, this is my Controller:
PHP Code:
public function index()
  {
    $pager = \Config\Services::pager();

    $articlesModel = new ArticlesModel();
    $categoriesModel = new CategoriesModel();

    $data = [
      'title'      => 'articoli',
      'articles'   => $articlesModel->orderBy('id''DESC')->findAll(),
      'categories' => $categoriesModel->findAll(),
      'pag'        => $articlesModel->orderBy('id''DESC')->paginate(5),
      'pager'      => $articlesModel->pager,
    ];

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

This is my view:

PHP Code:
<ul class="uk-pagination">
  <?php foreach ($pager->links() as $link): ?>
    <li><a href=""></a></li>
  <?php endforeach; ?>
</ul> 

I have this error: Invalid argument supplied for foreach()

Thanks for help me!  Blush
Reply

#2
To show the pagination links you just do this:

PHP Code:
<?= $pager->links();?>

You use the foreach to output your database records.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
Thanks for reply!

If i want use custom classes for <ul> and <li> and to have the button NEXT and PREV?

I read this but doesn't work: https://codeigniter.com/user_guide/libra...g-the-view
Reply

#4
You need to create your own I have 2 custom bootstrap 4 pagers here I will post them for you

1) you need to edit app/Config/Pager and add the two lines below to the $templates array.

PHP Code:
'bs_full'        => 'Pagers\bs_full',
'bs_simple'      => 'Pagers\bs_simple'

2) Create a folder under your Views folder called Pagers and add these two views to ir

app/Views/Pagers

bs_full.php

PHP Code:
<?php

/**
 * bs_full.php - Bootstrap 4 full Pager
 * @var \CodeIgniter\Pager\PagerRenderer $pager
 */

$pager->setSurroundCount(2);
?>

<nav aria-label="<?= lang('Pager.pageNavigation'?>">
    <ul class="pager pagination justify-content-center">
        <?php if ($pager->hasPreviousPage()) : ?>
            <li class="page-item">
                <a class="page-link" href="<?= $pager->getFirst() ?>" aria-label="<?= lang('Pager.first'?>">
                    <span aria-hidden="true"><?= lang('Pager.first'?></span>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" 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="page-item active"' '' ?>>
                <a class="page-link" href="<?= $link['uri'?>">
                    <?= $link['title'?>
                </a>
            </li>
        <?php endforeach ?>

        <?php if ($pager->hasNextPage()) : ?>
            <li class="page-item">
                <a class="page-link" href="<?= $pager->getNextPage() ?>" aria-label="<?= lang('Pager.next'?>">
                    <span aria-hidden="true"><?= lang('Pager.next'?></span>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" 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

bs_simple.php

PHP Code:
<?php
/**
 * bs_simple.php - Bootstrap 4 Simple Pager
 * @var \CodeIgniter\Pager\PagerRenderer $pager
 */

$pager->setSurroundCount(0);
?>
<nav aria-label="Page Results">
    <ul class="pager pagination justify-content-center">
        <li <?= $pager->hasPrevious() ? 'class="page-item active"' 'class="page-item disabled"' ?>>
            <a class="page-link" href="<?= $pager->getPrevious() ?? '#' ?>" aria-label="<?= lang('Pager.previous'?>">
                <span aria-hidden="true"><?= lang('Pager.newer'?></span>
            </a>
        </li>
        <li <?= $pager->hasNext() ? 'class="page-item active"' 'class="page-item disabled"' ?>>
            <a class="page-link" href="<?= $pager->getnext() ?? '#' ?>" aria-label="<?= lang('Pager.next'?>">
                <span aria-hidden="true"><?= lang('Pager.older'?></span>
            </a>
        </li>
    </ul>
</nav> 


to load them in the controller and view is a little different

Controller:

PHP Code:
->paginate(3'group1'), 


And in the view you load it like this:

PHP Code:
<!-- Pagination -->
<
div class="pagination justify-content-center mb-4">
    <?
php if ( ! empty($pager)) {
        //echo $pager->simpleLinks('group1', 'bs_simple');
        echo $pager->links('group1''bs_full');
    } 
?>
</div>

<br> 

See the group1 in the Controller code and the View code.

The ( Justify-content-center ) In the <ul> is to center both Pagers you can remove it
if you do not want them centered.

That will show you how it is done.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
Thumbs Up 
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.