Jump to content

Pagination with field = value


rooofl
 Share

Recommended Posts

Hello,

I want to paginate my keyword = value pages.

This works well when I only paginate those pages:

$fonts = $pages->get("/fonts/")

But it stops working when I try to paginate

$fonts = $pages->get("/fonts/")->find("keywords=mykeyword");

My pagination code looks like:

$fontlist = $fonts->find("id>1, limit=6, draft=0, sort=-created");
$pagination = $fontlist->renderPager(array(
  'nextItemLabel' => "▶",
  'previousItemLabel' => "◀",
  'linkMarkup' => "<a rel='prefetch' href='{url}'>{out}</a>"));
echo $input->pageNum;
foreach($fontlist as $font){
…

“Allow page number” is checked on both fonts ant keywords templates.

Link to comment
Share on other sites

Pagination only works with wireArrays taken directly from the database. When you do $pages->get("/fonts/")->find("keywords=mykeyword") you're filtering a wireArray that is already in memory. Try to find a way to form the same array in one go, for example: $pages->find("has_parent=/fonts/, keywords=mykeyword")

  • Like 2
Link to comment
Share on other sites

@diogo

I was about to write the same thing earlier today, but it's not right. It would be right it were PageArray::find, but $pages->get("/fonts/") returns a Page and not an array of pages. Page::find() does essentially what you suggested. It rewrites the selector to include "has_parent". So no need to do this manually in this case.

@rooofl

It's strange that it would work without the find() method as you cannot paginate the result of $pages->get("/fonts/"), because it only returns a single page. 

  • Like 1
Link to comment
Share on other sites

@LostKobrakai

As you can see in this demo the home page pagination works fine with $pages->get("/fonts/"), I must admit I don’t know why exactly. Here is the code of my home page (working) and the one for my pagination, finally the code of the keyword page.

I am open to any other solutions, idea or examples, as I am not an expert, maybe I missed something.

Link to comment
Share on other sites

Now it makes sense, didn't grasp the $fonts in the different codeparts. Essentially you're using this:

$fontlist = $pages->get("/fonts/")->find("id>1, …");

vs. 

$fontlist = $pages->get("/fonts/")->find("keywords=mykeywords")->find("id>1, …"); 

There it's exactly the difference diogo and I did talk about: The first one is classwise Pages > Page > find(), which works for pagination and the second one is Pages > Page > PageArray > find(), which doesn't.

You'd need to build it like this:

$parents = $pages->get("/fonts/")->find("keywords=mykeywords");
$fontlist = $pages->find("parent={$parents}, id>1, …");

Edit:

To explain the difference a little more. Pages::find() and Page::find() are both searching new pages, which will be paginated. PageArray::find does not search for new pages, it just returns a filtered subset of itself, which is not automatically set up for pagination. 

  • Like 2
Link to comment
Share on other sites

@LostKobrakai, you're right, my distraction. Still, my suggestion should work.

@rooofl, I already used that site to find fonts :D, great work! Actually, this isn't even the first project of yours that I used, have a look at my bookmarks bar ;)

post-88-0-03023600-1431683769_thumb.png

Link to comment
Share on other sites

// on home.php
$fonts = $pages->get("/fonts/");

//on fontlist.inc
$fontlist = $fonts->find("id>1, limit=6, draft=0, sort=name");

// so this is nothing else than
$pages->get("/fonts/")->find("id>1, limit=6, draft=0, sort=name")

//which is the same as:
$pages->find("parent=/fonts/, id>1, limit=6, draft=0, sort=name")

//and thus works as expected, and i don't think you need the id part of the selector

For the keyword pagination the page count seems alright but you get the same fonts on all page numbers. Correct?

I think this has to do with the fact that you already do a find in keyword.php:

$fonts = $pages->find("keywords=$page");

And you then include fontlist.inc which you again feed $fonts

$fontlist = $fonts->find("id>1, limit=6, draft=0, sort=name");

I can see how this could go wrong, see http://processwire.com/api/modules/markup-pager-nav/ , under " Are there any side effects"

Edit

Too late again, don't be grabbing some coffee before writing a post :)

Maybe my conclusion was wrong also

Link to comment
Share on other sites

Solved! Thank you all.

My code is:

keyword.php:

$fonts = $pages->get("/fonts/");
$filter = "keywords=$page";
include("./fontlist.inc");

fontlist.inc

$fontlist = $fonts->find("$filter,limit=6, draft=0, sort=-created");
$pagination = $fontlist->renderPager(array(
…

This way it allows me to set limit, draft, and sort in the font list template whatever the filters.

@diogo

I am really glad you use and like my projects! It is always a pleasure to see that my work can be useful. Thanks to you, Use & Modify will have keyword pagination bug free!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...