Welcome Guest, Not a member yet? Register   Sign In
Pagination : Call to undefined method setSurroundCount()
#1

(This post was last modified: 04-27-2020, 10:23 AM by David Goncalves.)

Hello,

I'm trying to implement a pagination in my view but when I call the setSurroundCount function it gives me an error "Call to undefined method CodeIgniter\Pager\Pager::setSurroundCount() "
Does someone knows why ?

Here is my controller :

PHP Code:
function index($limit=100) {

            // library('pagination');

            $model = new \App\Models\TransactionModel();

            $data = [
                'title' => "Transactions",
                'transactionsHTMLTable' => $this->formatTransactionsAsHTMLTable($model->paginate($limit)),
                'pager' => $model->pager
            
];

            // $data["transactionsHTMLTable"] = $this->formatTransactionsAsHTMLTable($model->transactions($limit, $offset));

            echo view('top'$data);
            echo view('transactions'$data);
            echo view('bottom');
        


Here is my view ;

PHP Code:
<h3>Transactions</h3>
<?
php
    
echo $transactionsHTMLTable;
    
// echo $pager->links();
    
$pager->setSurroundCount(2);
?>

<?php  ?>

<nav aria-label="Page navigation">
    <ul class="pagination">
    <?php if ($pager->hasPrevious()) : ?>
        <li>
            <a href="<?= $pager->getFirst() ?>" aria-label="First">
                <span aria-hidden="true">First</span>
            </a>
        </li>
        <li>
            <a href="<?= $pager->getPrevious() ?>" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
    <?php endif ?>

    <?php foreach ($pager->links() as $link) : ?>
        <li <?= $link['active'] ? 'class="active"' '' ?>>
            <a href="<?= $link['uri'?>">
                <?= $link['title'?>
            </a>
        </li>
    <?php endforeach ?>

    <?php if ($pager->hasNext()) : ?>
        <li>
            <a href="<?= $pager->getNext() ?>" aria-label="Previous">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
        <li>
            <a href="<?= $pager->getLast() ?>" aria-label="Last">
                <span aria-hidden="true">Last</span>
            </a>
        </li>
    <?php endif ?>
    </ul>
</nav> 


I took everything from the Codeigniter 4 documentation.

If I echo $pager->links() it works. So the $pager is here but the functions setSurroundCount getFirst, getNext, ... doesn't work.
Can someone help me ?
Reply
#2

(04-27-2020, 08:13 AM)David Goncalves Wrote: Hello,

I'm trying to implement a pagination in my view but when I call the setSurroundCount function it gives me an error "Call to undefined method CodeIgniter\Pager\Pager:ConfusedetSurroundCount() "
Does someone knows why ?

Here is my controller :

PHP Code:
function index($limit=100) {

            // library('pagination');

            $model = new \App\Models\TransactionModel();

            $data = [
                'title' => "Transactions",
                'transactionsHTMLTable' => $this->formatTransactionsAsHTMLTable($model->paginate($limit)),
                'pager' => $model->pager
            
];

            // $data["transactionsHTMLTable"] = $this->formatTransactionsAsHTMLTable($model->transactions($limit, $offset));

            echo view('top'$data);
            echo view('transactions'$data);
            echo view('bottom');
        


Here is my view ;

PHP Code:
<h3>Transactions</h3>
<?
php
 
echo $transactionsHTMLTable;
 
// echo $pager->links();
 
$pager->setSurroundCount(2);
?>

<?php  ?>

<nav aria-label="Page navigation">
    <ul class="pagination">
    <?php if ($pager->hasPrevious()) : ?>
        <li>
            <a href="<?= $pager->getFirst() ?>" aria-label="First">
                <span aria-hidden="true">First</span>
            </a>
        </li>
        <li>
            <a href="<?= $pager->getPrevious() ?>" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
    <?php endif ?>

    <?php foreach ($pager->links() as $link) : ?>
        <li <?= $link['active'] ? 'class="active"' '' ?>>
            <a href="<?= $link['uri'?>">
                <?= $link['title'?>
            </a>
        </li>
    <?php endforeach ?>

    <?php if ($pager->hasNext()) : ?>
        <li>
            <a href="<?= $pager->getNext() ?>" aria-label="Previous">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
        <li>
            <a href="<?= $pager->getLast() ?>" aria-label="Last">
                <span aria-hidden="true">Last</span>
            </a>
        </li>
    <?php endif ?>
    </ul>
</nav> 


I took everything from the Codeigniter 4 documentation.

If I echo $pager->links() it works. So the $pager is here but the functions setSurroundCount getFirst, getNext, ... doesn't work.
Can someone help me ?

If I am asked what is the most important thing that happened in my life, without hesitation, I answer: "To be literate." I believe that about 70% success in life depends on it. If I am asked what the school is for, also without hesitation, I reply: "It is a place where we learn to learn." Obviously, I am not the owner of the truth, but my opinion.
I am saying this because I went through the same experience as pal @david gonçalves. I thought of suggesting to him a more careful reading of the manual, but that is not the case.

I believe that when writing a manual the first thing beyond the objective is to determine who the target audience will be. The higher the level, the more summarized it can be, implicit values and between the lines, etc., but everything that deserves to be done deserves to be done well, so it should be done with the head of the reader in mind.

Pagination, in the manual: https://codeigniter.com/user_guide/libra...ation.html

Customizing the Links

"View" Configuration(*)

(*)Depending on who reads and the knowledge of the framework, "View" is the V in MVC, nothing more, getting worse if English is not the native language.

When the links are rendered out to the page, they use a view file to describe the HTML. You can easily change the view that is used by editing app/Config/Pager.php:

public $ templates = [
    'default_full' => 'CodeIgniter\Pager\Views\default_full',
    'default_simple' => 'CodeIgniter\Page\Views\default_simple'
];

Why not show the page so that we have a better idea of what should be done?

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',
 
'default_full' => 'App\Views\Pagers\wdeda_full',  
 
'default_simple' => 'App\Views\Pagers\wdeda_simple'// <= if in the future I need it, it’s ready.
 
 
];

 
/*
 |--------------------------------------------------------------------------
 | Items Per Page
 |--------------------------------------------------------------------------
 |
 | The default number of results shown in a single page.
 |
 */
 
public $perPage 20;
 public 
$segment 1;


Creating the View //Watch out! It's another view.
PHP Code:
<?php $pager->setSurroundCount(2?>

<nav aria-label="Page navigation">
    <ul class="pagination">
    <?php if ($pager->hasPrevious()) : ?>
        <li>
            <a href="<?= $pager->getFirst() ?>" aria-label="First">
                <span aria-hidden="true">First</span>
            </a>
        </li>
        <li>
            <a href="<?= $pager->getPrevious() ?>" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
    <?php endif ?>

    <?php foreach ($pager->links() as $link) : ?>
        <li <?= $link['active'] ? 'class="active"' '' ?>>
            <a href="<?= $link['uri'?>">
                <?= $link['title'?>
            </a>
        </li>
    <?php endforeach ?>

    <?php if ($pager->hasNext()) : ?>
        <li>
            <a href="<?= $pager->getNext() ?>" aria-label="Previous">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
        <li>
            <a href="<?= $pager->getLast() ?>" aria-label="Last">
                <span aria-hidden="true">Last</span>
            </a>
        </li>
    <?php endif ?>
    </ul>
</nav> 

It is in App\Views\Pagers\wdeda_full.php. It has nothing to do with the data contained in the View, the page itself. All code is only for:
First 1 2 3 Last

These templates are located in /system/pager/views, but only if you like to live dangerously do you want to change them.

I've been using CodeIgniter since 2014 and every time I see a topic as "why use codeigniter?" I remember my reasons for using it. Nothing is more contagious than the example. Below, one of the reasons, a search page with pagination in 2014, without CI:
PHP Code:
<?php
if (!isset($_GET['consult'])) {
  header("Location: /");
  exit;
}

$search mysql_real_escape_string($_GET['consulta']);

//records per page
$per_page 20;

$condictions "(`active` = 1) AND ((`ttitle` LIKE '%{$search}%') OR ('%{$search}%'))";
$sql "SELECT COUNT(*) AS total FROM `noticias` WHERE {$condictions}";

$query mysql_query($sql);

$total mysql_result($query0'total');

$pages =  (($total $per_page) > 0) ? (int)($total $per_page) + : ($total $per_page);

if (isset(
$_GET['page'])) {
  $page = (int)$_GET['page'];
} else {
  $page 1;
}
$page max(min($pages$page), 1);
$offset = ($page 1) * $per_page;

$sql "SELECT * FROM `news` WHERE {$condictions} ORDER BY `register` DESC LIMIT {$offset}{$per_page}";

$query mysql_query($sql);

echo 
"Results ".min($total, ($offset 1))." - ".min($total, ($offset $per_page))." of ".$total." found results  for '".$_GET['search']."'";

echo 
"<ul>";
while (
$result mysql_fetch_assoc($query)) {
  $title $result['title'];
  $text $result['text'];
  $link '/news.php?id=' $result['id'];
  
  
echo "<li>";
    echo "<a href='{$link}'>";
      echo "<h3>{$titl}</h3>"
      echo "<p>{$text}</p>"
    echo "</a>";
  echo "</li>";
}
echo 
"</ul>";


if (
$total 0) {
  for ($n 1$n <= $pages$n++) {
    echo "<a href='search.php?consult={$_GET['consult']}&page={$n}'>{$n}</a>";
  }


Please understand that it is a constructive criticism and I hope, if possible, that in the next version, 4.03, some changes will be made.
I am very proud to belong to this community.
Nothing compares to us.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB