Forum for discussing general topics related to Couch.
4 posts Page 1 of 1
I have looked through the documentation and I can't seem to see if there is anyway of ouputting a field in a random order.

I am planning to use cloned pages to create a list of testimonials. I would like to output 1 randomly into the sidebar. The testimonial would change as you clicked from page to page through the website.

Is there anyway to achieve this?

thanks.
Hi,

Randomizing the output of pages tag was a deliberate omission. The idea was that almost all the sites will (or should) have caching turned on and with cached pages since the same static page would be served most of the times, the random feature would be useless.

However if are not using the caching feature, for your case there can be a little hack to achieve what you desire.
Please take a look at the following snippet -
Code: Select all
<cms:pages masterpage='testimonials.php' limit='1' offset="<cms:php>echo(rand(1,4));</cms:php>">
   ....

</cms:pages>

The 'offset' parameter is used to skip a certain number of pages before listing the pages.
We are using PHP to set it to a random number between 1 and 4 (inclusive).
The code above has a 'limit' of 1 so it will always show only one page.
Thus the code above will always show one of the latest 5 pages after skipping a random number of pages. This causes a different testimonial to be displayed every time.

The '4' in the code above expects you to have at least 5 pages in your testimonials. You can tweak it to suit your case.

Hope this helps. Please let me know.
Thanks for the quick response.

I will give the code you supplied a go. I might also look into the cache feature you mention. Does turning it off generally slow the site done considerably?
...the cache feature you mention. Does turning it off generally slow the site done considerably?

Well, definitely not considerably but certainly eases the stress on the web server doling out the pages. Good to have on shared hosts.
4 posts Page 1 of 1
cron