Random selection of items in a slot

Hello,

Does anyone know if it is possible to set up a slot so that it picks content items at random? For example, if you have hundreds of “product” content items in your Rhythmyx (6.5.2) system, and have set up a daily publishing schedule, how could you implement a “Product of the Day” box on the web site’s front page?

Thanks,

Andrew.

Andrew,

I don’t have the whole answer to this, but I’m pretty sure you could do this with $user.psoSlotTools.getSlotContents() and $tools.math.random().

If you specify a “floor” of 0 and a “ceiling” equal to the size of the slot contents, then the random() function should give you a whole number that matches some item in the slot.

You’ll probably have to play with it some to get the exact syntax right.

Dave

Thanks Dave, that sounds reasonable. At this point we just wanted to know that it is possible in Rhythmyx. I’ll try to remember to post back here when I try to implement it.

Andrew.

Here is the code I have developed. It can be used between an #initslot() and a #endslot():


#set($num_matching_items = $sys.currentslot.relresults.size())##

#if($num_matching_items > 0)##
	<ul>
	## Create a copy of the results arraylist, so we can remove matches as we go, to prevent 
	## randomly picking the same item twice. The foreach loop fails if this is done to the original $sys.currentslot.relresults arraylist
	#set($cloned_results = $sys.currentslot.relresults.clone())##

	## Loop thru all matching items. In each iteration we are going to randomly pick another item to display
	## and ignore $relresult. If there was a while loop in Velocity, this would be more efficient
	#foreach($relresult in $sys.currentslot.relresults)##
		#if($max_results > 0)##
			<li>
				## Pick an item at random
				#set($random_index = $tools.math.random(0,$num_matching_items))##

				## Display the randomly chosen item
				#slotItem($cloned_results.get($random_index))##

				## Remove item from the cloned arraylist, so it cannot be picked again (enclose 
				## in if statement to prevent outputting the removed item
				#if($cloned_results.remove($random_index)) #end##

				## Decrement counter to keep track of the number of items displayed
				#set($max_results = $tools.math.sub($max_results,1))##

				## Decrement upper limit so the random number next time will be one of the remaining items not yet displayed
				#set($num_matching_items = $tools.math.sub($num_matching_items,1))##
			</li>
		#end##
	#end##
	</ul>
#end##

$max_results needs to be set to an integer less than the number of items (otherwise you get everything, and only the order is random.)

Great work. Now I can junk the Javascript I was trying to create, in the thread on numbering items in a list slot. To insert a random image on a home page, I added images to a list slot and inserted your code, minus some blank lines, into the template. To clarify the process, I’ve added the initslot here and set the max_results:

#initslot("sList" "")##
	## $max_results must be an integer. If it isn't less than the number of items, you get all items, but in random order.)
#set($max_results = 1)##
#set($num_matching_items = $sys.currentslot.relresults.size())##
etc.
...

and ended with #endslot(“sList”)##