简体   繁体   English

使用Pagerfanta对混洗后的数组进行分页

[英]Paginate a shuffled array with Pagerfanta

I have a page that displays 6 items (institutions in my case) on each paginated section of the page (therefore each page). 我有一个页面,在该页面的每个分页部分(因此每个页面)上显示6个项目(在我的情况下是机构)。 I display the items in a random order using shuffle . 我使用shuffle随机显示项目。 I use Symfony 4 and Pagerfanta for the pagination. 我使用Symfony 4和Pagerfanta进行分页。 The problem I face is that every time I go to the next page, my query is being shuffled again. 我面临的问题是,每当我转到下一页时,我的查询就会再次被打乱。 Randomizing the order is not the problem, the problem is this happens every time the user goes to the next page. 随机化顺序不是问题,问题在于每次用户转到下一页时都会发生。 It should only happen on the first page and remain in that order. 它应该只在第一页上发生并保持该顺序。 I have read a similar question here, but the problem is that all solutions seem to imply a completely new paginator. 我在这里读过一个类似的问题 ,但是问题是所有解决方案似乎都暗示着一个全新的分页器。 Is there a method to use both pagerfanta and solve my problem? 有没有办法同时使用pagerfanta和解决我的问题?

My code right now: 我现在的代码:

InstitutionController.php InstitutionController.php

 // I query for the list of items (institutions)
 $q = $request->query->get('q');
 $query = $institutionRepository->searchAndSortPublished($q);

 // I randomize the order of institutions
 shuffle($query);

 $pagerfanta = $paginationHelper->paginate($request, $query, 6);

 return $this->render('institution/index.html.twig', [ 'paginator' => $pagerfanta, 'institutions' => $query, 'q' => $q]);

PaginationHelper.php PaginationHelper.php

//...
    public function paginate($request, $array, $maxPerPage = 10)
{
    $page = $request->query->get('page', 1);
    $adapter = new ArrayAdapter($array);
    $pagerfanta = new Pagerfanta($adapter);
    $pagerfanta->setMaxPerPage($maxPerPage);
    $pagerfanta->setCurrentPage($page);

    return $pagerfanta;
}

Live example of my problem 我的问题的现场例子

If you are randomising the result, then the only real way would be to do your DB call on the first time, then store the rows (or just ids) in a session var or some other storage. 如果要随机化结果,那么唯一的真实方法是第一次进行数据库调用,然后将行(或仅ID)存储在会话var或其他存储中。 Then you can refer to that result each time. 然后您可以每次参考该结果。 Might also actually speed your page up! 实际上还可以加快您的页面速度!

Note that even if you had a seeded shuffle, you'd still need to fetch every record first. 请注意,即使您已经播种了种子,您仍然需要首先获取每个记录。

I suppose that sometimes institutions repeat themselves several times on different pages and this is the problem. 我想有时候机构会在不同的页面上重复几次,这就是问题所在。

The ad hoc solution is to shuffle institutions only on the given page. 临时解决方案是仅在给定页面上改组机构。 We will avoid repeating institutions on several pages (on the first page will always be the same institutions, but in different order). 我们将避免在几页上重复机构(第一页上始终是相同的机构,但顺序不同)。 Users will be able to see all items. 用户将能够看到所有项目。

For example, you can do this with the new adapter: 例如,您可以使用新适配器进行此操作:

ShuffleArrayAdapter.php ShuffleArrayAdapter.php

class ShuffleArrayAdapter extends ArrayAdapter
{
    public function getSlice($offset, $length)
    {
        return shuffle(array_slice($this->array, $offset, $length));
    }
}

I am not sure if this solution suits you, but it's the fastest. 我不确定该解决方案是否适合您,但这是最快的。

I've used Delboy1978uk's suggestion, storing the random order in a session variable. 我使用了Delboy1978uk的建议,将随机顺序存储在会话变量中。 I just wanted to show how this is done for those who are running into this problem as well. 我只是想向那些也遇到此问题的人展示如何做到这一点。 PaginationHelper.php is left untouched, the controller now looks like this: PaginationHelper.php保持不变,控制器现在看起来像这样:

//..

$query = $institutionRepository->findAll();

// set the random order in a session variable if it hasn't been set yet.
if (!$session->has('institution_order')) {
    shuffle($query);

    // set the session variable.
    $session->set('institution_order', $query);
}

// the random order per session per user.
$radomizedOrder = $session->get('institution_order');
$pagerfanta = $paginationHelper->paginate($request, $radomizedOrder, 6);

return $this->render('institution/index.html.twig', [ 'paginator' => $pagerfanta ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM