Jump to content

Load more random pages


cst989
 Share

Recommended Posts

What would you consider a good, clean way to append a list of pages retrieved randomly, with more random pages (excluding those you've got already)?

Eg you start with

$pages->find("template=gallery-detail, sort=random, limit=100");

You output this to your template, then you have a "load more" button. This works through ajax.

Now within the ajax, you need to select another 100, but exclude those you retrieved the first time. So I'm thinking you need to post all the pages you've got already, or maybe store in a session? And then exclude them from your next find(). But I imagine that would be a bit slow, excluding 100 pages by ID? Is there a more efficient way?

Link to comment
Share on other sites

If you want to make sure there are no duplicates, excluding them by ID is the way to go. There shouldn't be any performance issues - excluding one ID or a hundred shouldn't make any noticable difference for the generated database query. You can save the first result to the session as a string, and then include it in your selector.

// first result
$randomPages = $pages->find("template=gallery-detail, sort=random, limit=100");
$session->set('current-random-pages', (string) $randomPages);

// later, in an ajax call
$previous = $session->get('current-random-pages');
$randomPagesWithoutPrevious = $pages->find("template=gallery-detail, sort=random, limit=100, id!={$previous}");
// add the new pages to the list so you can load more multiple times
$session->set('current-random-pages', $previous . '|' . $randomPagesWithoutPrevious);

Quick and untested, might need some adjustments and context, but you get the idea 🙂

If you want to go stateless, you can also include the ID-list in the ajax call. By the way, casting a PageArray to string creates a pipe-seperated list of IDs (e.g. 2|5|28|278), this can go right into the query and is a good format to save the IDs to the session or include them in the ajax call.

  • Like 4
Link to comment
Share on other sites

The issue of paginating results when sort=random has come up before.

Ryan suggested finding all the IDs with a random sort, then loading pages from slices of that array. His example assumes normal pagination but could be adapted for ajax loading.

$limit = 10;
$start = $limit * ($input->pageNum()-1);
$ids = $session->get('ids');
if(!$ids) $ids = $pages->findIDs("template=project, sort=random");         
$session->set('ids', $ids); 
$a = array_slice($ids, $start, $limit); 
if(!empty($a)) {
  $items = $pages->getByID($a)->setStart($start)->setLimit($limit)->setTotal(count($ids));
  echo $items->implode("<li>{title}</li>") . $items->renderPager();
}

 

  • Like 4
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...