Tutorial: Output records in random order

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

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: [virgodesign] Tutorial: Output records in random order

By Dave - March 10, 2008

Yes, that's pretty much it. Then filtering the results with a url search -or- where clause like this:

viewer.php?category=Solutions

-or-

$options['where'] = "category = 'solutions'";

Hope that helps, let me know if you need any more details.
Dave Edis - Senior Developer
interactivetools.com

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