eZ Community » Forums » Setup & design » how to fetch random content (3.8)
expandshrink

how to fetch random content (3.8)

how to fetch random content (3.8)

Tuesday 16 May 2006 9:41:08 am - 8 replies

Hello,

I noticed 3.8 haves a new "rand" operator.

My questions is how to get random content using the sample template code:

	$photos=fetch( 'content', 'tree', hash ( parent_node_id, 170,					
                                                               limit, 6,
                                                               class_filter_type, include,
                                                               class_filter_array, array( 'image' ),
                                                               sort_by, array( 'published', false() ) )  )                                               

The documentation pages doest include any reference to this new operator.

Any help ?

Modified on Tuesday 16 May 2006 3:54:01 pm by Alexandre Cunha

Tuesday 16 May 2006 2:19:08 pm

A quick look in ezpublish/lib/eztemplate/classes/eztemplateartihmeticoperator.php tells me that it's just a wrapper for the PHP mt_rand() function:
http://www.php.net/manual/en/function.mt-rand.php
int mt_rand ( [int min, int max] )
When called with no parameters it returns an integer between 0 and MAX_INT (2^31-1), when called with two parameters it returns an integer in the range min, max.

So to get a random object you can do something like this:

{$max=fetch('content', 'tree_count', hash(parent_node_id, 2))}
{$node=fetch('content', 'tree', hash(parent_node_id, 2, offset, rand(0,$max), limit, 1))}

Add class and/or attribute filters to both fetches as appropiate.

Modified on Tuesday 16 May 2006 2:20:46 pm by Marc Boon

Tuesday 16 May 2006 3:34:21 pm

Hi marc,

Very elegant solution, IMO.

Thanks for sharing the tip!

X+

Wednesday 21 June 2006 4:14:31 pm

That's fine when you want one random node/object/class/whatever, but is there a way to get 10 random nodes in a single fetch?

Sunday 25 June 2006 1:29:52 am

I can't think of any way to fetch multiple random nodes with a single fetch. The only solution that comes to mind is to use a while loop to pushes randomly selected nodes onto an array:

http://ez.no/doc/ez_publish/techn...ate_control_structures/looping/while
http://ez.no/doc/ez_publish/techn...ce/template_operators/arrays/prepend

You probably want to make sure you don't end up with duplicate elements too:

http://ez.no/doc/ez_publish/techn...nce/template_operators/arrays/unique

...that should help you end up with a nice array of 10 randomly selected, unique nodes.

Good luck
Matthew

Thursday 19 April 2007 11:33:57 am

Hi,

Marc Boon's solution is nice, but I think you should "dec" $max before calling the rand operator inside the fetch.

bye !

Friday 20 April 2007 1:16:23 pm

Probably we need a {def.....} like

{def $max=fetch('content', 'tree_count', hash(parent_node_id, 2))}
{def $node=fetch('content', 'tree', hash(parent_node_id, 2, offset, rand(0,$max), limit, 1))}

Friday 23 May 2008 1:24:57 pm

Hi,

To work correctly, I need to add this in the code:

{def $max=sum(fetch('content', 'tree_count', hash('parent_node_id', 175)),-1)}
{def $random_project=fetch('content', 'tree', hash('parent_node_id', 175, 'offset', rand(0,$max), 'limit', 1))}

Without -1, I have sometimes an empty value because the offset generated is too high.

Monday 28 June 2010 11:10:12 pm

You could also try to add shuffle to the end of your fetch-string, something like:

{$photos=fetch( 'content', 'tree', hash ( parent_node_id, 170,     
                                                               limit, 6,
                                                               class_filter_type, include,
                                                               class_filter_array, array( 'image' ),
                                                               sort_by, array( 'published', false() ) )  )|shuffle} 
expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from