Jump to content

[SOLVED] How to paginate PageArray after constructing array by adding pages to PageArray?


Violet
 Share

Recommended Posts

I have constructed a PageArray with pages in a specific order, as shown in this code below. I then try to apply setLimit and setStart to the PageArray to paginate it, but that doesn't seem to work. When I do that, the entire contents of the PageArray (5 items) are shown identically on both page 1 and page 2, instead of the first 3 items on page 1 and the last 2 on page 2. Where have I gone wrong with my code?

 $glinks = new PageArray();
 $recentarticle = $pages->findOne("template=article, sort=-published");
 $glinks->add($recentarticle);
 $glinks->import($homepage->desiredpages);
 $allarticles = $pages->find("template=article, sort=-published");
 $allarticles->remove($recentarticle);
 $glinks->import($allarticles);
 $glinks->setLimit(3);
 $glinks->setStart(0);

foreach ($glinks as $onealink) {
// ... here I display info about each item ...
}

 echo $glinks->renderPager(array(
    'nextItemLabel' => $nexttext,
    'previousItemLabel' => $prevtext,
    'listMarkup' => "<ul>{out}</ul>",
    'itemMarkup' => "<li class='{class}'>{out}</li>",
    'linkMarkup' => "<a href='{url}'>{out}</a>",
    'currentItemClass' => "active"
  )); 

The template allows page numbers, I checked it, and I've set it to page URL's requiring a trailing slash, but I've also tried it with page URL's set as either/does not matter. I've hunted around the PW API and forum and I can't seem to figure out what I did wrong.

I've gotten pagination working fine in other situations, e.g. on a different template where I obtain the PageArray from a simple $pages->find with a limit in the selector. But when I instead build the array by adding to a PageArray like in my code example, then pagination doesn't seem to work. Why is this?

Edited by Violet
in code, added the commented out area where I display info about each item
Link to comment
Share on other sites

Thanks @Zeka for mentioning about the start set to 0 and pointing me in the right direction. I am working my way through the thread you mentioned. I was hoping to not have to slice the array and keep track of the start point for each page etc. I wanted to just have PW handle everything automatically like it would for a simple $pages->find with a limit on it. But it looks like I will have to do something a little more complicated, according to the thread. 

I have been making some progress by using some of the approaches in the thread you linked thanks, I'm hoping to get to a fully workable situation eventually.

Link to comment
Share on other sites

  • Violet changed the title to [SOLVED] How to paginate PageArray after constructing array by adding pages to PageArray?

Updating on the final situation - a huge thank you to @Zekafor pinpointing the problem and getting me to the correct solution. I wound up using the solution you mentioned by @ryan in your thread you quoted. So the final code is:

$flinks = new PageArray();
$flinks->setDuplicateChecking(false);  
$recentarticle = $pages->findOne("template=article, sort=-published");
$flinks->add($recentarticle);
$flinks->import($homepage->desiredpages);
$allarticles = $pages->find("template=article, sort=-published");
$allarticles->remove($recentarticle);
$flinks->import($allarticles);
$items_per_page = 3;
$start = ($input->pageNum - 1) * $items_per_page;
$flinks->setLimit($items_per_page);
$flinks->setStart($start);
$glinks = $flinks->slice($start, $items_per_page);

foreach ($glinks as $onealink) {
// ... here I display info about each item ...
}

 echo '<a href="#afterpag" class="skip">' . $pagenavend . '</a>';
  echo $flinks->renderPager(array(
    'nextItemLabel' => $nexttext,
    'previousItemLabel' => $prevtext,
    'listMarkup' => "<ul>{out}</ul>",
    'itemMarkup' => "<li class='{class}'>{out}</li>",
    'linkMarkup' => "<a href='{url}'>{out}</a>",
    'currentItemClass' => "active"
)); 

I tested it with different values of $items_per_page and it all worked fine. Thank you so much! I was thrilled to get it working so quickly! I have marked the topic as solved.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...