We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Pagination and setVar in IndexController

Hello,

in my database, i have stored 10000 datasets. I want to split them into 100. The problem that i have is, the variable in my view.

index.thumb is undefinied, but when i set the variable with setVar it is working, but 10000 data are loading.

When i put the paginator into a function, the var page is also undefinied.

this is my Model:

<?php

namespace Vokuro\Models;
use Phalcon\Mvc\Model;

class Index extends Model{

public $id;

public $link;

public $title;

public $tags;

public $thumb;
}

My Controller:

<?php
namespace Vokuro\Controllers;
use Phalcon\Mvc\Controller;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Tag;
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\Model\Query;
use Vokuro\Models\Index;

class IndexController extends Controller
{

public function indexAction()
{

    $this->assets
        ->collection('header')
        ->addCss('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css', false)
        ->addCss('https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css', false)
        ->addCss('https://fonts.googleapis.com/css?family=Josefin+Sans:400,300', false)
        ->addCss('plugins/venobox/venobox.css')
        ->addCss('css/style.css')
        ->addCss('css/media-queries.css');

    $this->assets
        ->collection('footer')
        ->addJs('https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js', false)
        ->addJs('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', false)
        ->addJs('plugins/venobox/venobox.js')
        ->addJs('js/index.js');

    $post  = Index::find();
    $this->view->setVar('index',$post);

    $numberPage = 1;
    if ($this->request->isPost()) {
        $query = Criteria::fromInput($this->di, 'Vokuro\Models\Index', $this->request->getPost());
        $this->persistent->searchParams = $query->getParams();
    } else {
        $numberPage = $this->request->get("page", "int");
    }

    $parameters = array();
    if ($this->persistent->searchParams) {
        $parameters = $this->persistent->searchParams;
    }
    $vids = Index::find($parameters);
    if (count($vids) == 0) {

        $this->flash->notice("The search did not find any profiles");

        return $this->dispatcher->forward(array(
            "action" => "index"
        ));
    }

    $paginator = new Paginator(array(
        "data" => $vids,
        "limit" => 10,
        "page" => $numberPage
    ));

    $this->view->page = $paginator->getPaginate();

}
}

And my View:

<div class="row itop25">
{% for index in index %}
<div class="col-md-2 col-xs-12 nopadding">
    <div class="embed-responsive embed-responsive-16by9">
        {{ tag_html("a", ["href":index.link, 'class' : 'venobox embed-responsive embed-responsive-16by9', 'data-gall' : 'vids',  'data-type':'iframe']) }}
        {{ image(index.thumb, "class":"img-responsive", "title":index.title) }}
        {{ tag_html_close("a") }}
    </div>
</div>
{% endfor %}

<div class="btn-group">
    {{ link_to("index/", '<i class="icon-fast-backward"></i> First', "class": "btn") }}
    {{ link_to("index?page=" ~ page.before, '<i class="icon-step-backward"></i> Previous', "class": "btn ") }}
    {{ link_to("index?page=" ~ page.next, '<i class="icon-step-forward"></i> Next', "class": "btn") }}
    {{ link_to("index?page=" ~ page.last, '<i class="icon-fast-forward"></i> Last', "class": "btn") }}
    <span class="help-inline">{{ page.current }}/{{ page.total_pages }}</span>
</div>

</div>

Thx for help



12.2k
Accepted
answer

Unfortunately you are doing it wrong :)


// In controller

$vids = Index::find($parameters);

$paginator = new Paginator(array(
        "data" => $vids,
        "limit" => 10,
        "page" => $numberPage
    ));

$this->view->setVar('page', $paginator->getPaginate());

// In volt view

{% for item in page.items %}

{% endfor %}

// And to use page numbers/total numbers
{{ page.current }}
{{ page.first }}
{{ page.before }}
{{ page.next }}
{{ page.last }}
{{ page.total_items }}
{{ page.total_pages }}

More of course here: https://docs.phalcon.io/en/latest/reference/pagination.html



59.6k

Hello mraspor,

now it works :-) Thank you.

Rgds Stefan