Forums / Setup & design / how to fetch random content (3.8)

how to fetch random content (3.8)

Author Message

Alexandre Cunha

Tuesday 16 May 2006 12:41:08 am

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 ?

http://AlexandreCunha.com

Marc Boon

Tuesday 16 May 2006 5:19:08 am

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.

Xavier Dutoit

Tuesday 16 May 2006 6:34:21 am

Hi marc,

Very elegant solution, IMO.

Thanks for sharing the tip!

X+

http://www.sydesy.com

Christian Johansen

Wednesday 21 June 2006 7:14:31 am

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?

Matthew Carroll

Saturday 24 June 2006 4:29:52 pm

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/technical_manual/3_8/reference/template_control_structures/looping/while
http://ez.no/doc/ez_publish/technical_manual/3_8/reference/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/technical_manual/3_8/reference/template_operators/arrays/unique

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

Good luck
Matthew

http://carroll.org.uk

Frédéric RAYMOND

Thursday 19 April 2007 2: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 !

Softriva .com

Friday 20 April 2007 4:16:23 am

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))}

Ludovic Gasc

Friday 23 May 2008 4:24:57 am

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.

Christopher Grondahl

Monday 28 June 2010 2: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} 

Chris

ez site: http://www.acem.no

Every day is Groundhog Day