Tutorial: Output records in random order

7 posts by 2 authors in: Forums > CMS Builder
Last Post: March 11, 2008   (RSS)

By Dave - December 3, 2007 - edited: December 11, 2007

One of the cool things about CMS Builder is that you don't need to know any PHP or MySQL to use it, but if you know even a little, you can do some pretty neat things.

We're actually using CMS Builder to power the "Random Quotes" section of our own website. All we did to have the quotes come up in random order was set the following option on the list viewer page:

$options['orderBy'] = "RAND()";

The orderBy option on the list page is actually passed directly to MySQL as the "ORDER BY" value in a SELECT query. MySQL has a built in function RAND() for getting random values. So all we need to do is combine the two and you get your output records in random order.

CMS Builder makes it really really easy to control exactly what is displayed, and what order. Whether you want to show the top 3 featured jobs on another page, random quotes, all the products between $100 and $250, or something else.

If there's something specific you want to do and you're not sure just post and we'll help you out.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Tutorial: Output records in random order

By avrom - March 9, 2008

Hi Dave,

I want to do exactly this, but with the option of pulling quotes randomly per category.

i.e. One category might be "Tips and Tricks" or another category might be "Solutions". So not only is a quote displayed randomly but also by category.

Is it as simple as just adding a drop-down field to select the category in the Field Viewer, and then sorting the result in the display ?

Cheers Dave,

Much thanks !
Avrom

Re: [Dave] Tutorial: Output records in random order

By avrom - March 11, 2008

Hi Dave,

That's great. I only need one quote per page, so no need to reorder the table, only to get a random field in the category from 1 to Max number of Records in that category. So how do I actually combine these 2 php queries together ? Also does there need to be a check for rand to max. records in the orderBy field ? Much thanks.

require_once "/www/htdocs/cmsAdmin/lib/viewer_functions.php";
$options['tableName'] = 'quotes';
$options['where'] = "category = 'solutions'";
$options['recordNum'] = "RAND()";
$record = getRecord($options);

Not sure how to integrate the random field. Am I on the right track ? Thanks so much

Avrom

Re: [virgodesign] Tutorial: Output records in random order

By Dave - March 11, 2008

Almost there, use the "list viewer" instead of the "page viewer". The list viewer page lets you specify sort order. Even though it's just a list of 1.

Give that a try and let me know how it goes.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Tutorial: Output records in random order

By avrom - March 11, 2008

Like this ?

require_once "/www/htdocs/cmsAdmin/lib/viewer_functions.php";
$options = array();
$options['tableName'] = 'quotes';
$options['titleField'] = 'category';
$options['viewerUrl'] = '';
$options['perPage'] = '';
$options['orderBy'] = "RAND()";
$options['pageNum'] = '';
$options['where'] = "category = 'solutions'";
$options['useSeoUrls'] = '';
list($listRows, $listDetails) = getListRows($options);
?>

Re: [virgodesign] Tutorial: Output records in random order

By Dave - March 11, 2008

Yes, but set these:

$options['perPage'] = '1';
$options['pageNum'] = '1';
Dave Edis - Senior Developer
interactivetools.com