 | |  |
 |

Dave
Staff
/ Moderator

Dec 3, 2007, 8:26 PM
Post #1 of 7
(1125 views)
Shortcut
|
|
Tutorial: Output records in random order
|
Can't Post
|
|
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
(This post was edited by Dave on Dec 11, 2007, 3:02 PM)
|
|
|  |
 |

virgodesign
User
Mar 8, 2008, 9:39 PM
Post #2 of 7
(726 views)
Shortcut
|
|
Re: [Dave] Tutorial: Output records in random order
[In reply to]
|
Can't Post
|
|
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
|
|
|  |
 |

Dave
Staff
/ Moderator

Mar 10, 2008, 9:55 AM
Post #3 of 7
(713 views)
Shortcut
|
|
Re: [virgodesign] Tutorial: Output records in random order
[In reply to]
|
Can't Post
|
|
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
|
|
|  |
 |

virgodesign
User
Mar 11, 2008, 12:18 PM
Post #4 of 7
(705 views)
Shortcut
|
|
Re: [Dave] Tutorial: Output records in random order
[In reply to]
|
Can't Post
|
|
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
|
|
|  |
 |

Dave
Staff
/ Moderator

Mar 11, 2008, 12:44 PM
Post #5 of 7
(704 views)
Shortcut
|
|
Re: [virgodesign] Tutorial: Output records in random order
[In reply to]
|
Can't Post
|
|
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
|
|
|  |
 |

virgodesign
User
Mar 11, 2008, 1:03 PM
Post #6 of 7
(700 views)
Shortcut
|
|
Re: [Dave] Tutorial: Output records in random order
[In reply to]
|
Can't Post
|
|
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); ?>
|
|
|  |
 |

Dave
Staff
/ Moderator

Mar 11, 2008, 1:05 PM
Post #7 of 7
(698 views)
Shortcut
|
|
Re: [virgodesign] Tutorial: Output records in random order
[In reply to]
|
Can't Post
|
|
Yes, but set these: $options['perPage'] = '1'; $options['pageNum'] = '1'; Dave Edis - Senior Developer interactivetools.com
|
|
|  |
|