XF 2.2 Limit and shuffle finder results

Orit

Active member
Hi
I have a finder for some categories and I want to shuffle the results and limit them.
Since I don't want to always return the first x of the results, I am not limiting in the finder itself.

My code is as follows:

PHP:
$categoryFinder = $this->finder('****')
    ->where('category_id', $categoryIds);

$categories = $categoryFinder->fetch();
$categories = $categories->shuffle();

$categories = $categories->slice(0, $limit);

I am wondering if there is a better (built in maybe?) way to do so?
(I did not find a way to do so through the template. Is there a way to limit results in a for loop inside the template?)

Thanks!
 
Hi
I have a finder for some categories and I want to shuffle the results and limit them.
Since I don't want to always return the first x of the results, I am not limiting in the finder itself.

My code is as follows:

PHP:
$categoryFinder = $this->finder('****')
    ->where('category_id', $categoryIds);

$categories = $categoryFinder->fetch();
$categories = $categories->shuffle();

$categories = $categories->slice(0, $limit);

I am wondering if there is a better (built in maybe?) way to do so?
(I did not find a way to do so through the template. Is there a way to limit results in a for loop inside the template?)

Thanks!
Have you tried to incorporate the template rand statement instead of PHP shuffle?
 
Have you tried to incorporate the template rand statement instead of PHP shuffle?
I don't think rand will work in this case.

The shuffle function randomises every element in the array, preserving the array content, whereas the rand function just spits out a random number between 2 values.
 
Back
Top Bottom