limit to first 6 records not working with order by rand()

5 posts by 2 authors in: Forums > CMS Builder
Last Post: July 12, 2011   (RSS)

By zaba - July 12, 2011

Hi I have a revolving news set on a home page (a jquery slider). I wanted it to grab the first 6 records in the news database and then push them randomly on the home page. However everything works except it is not pulling in the first 6 records and then delivering randomly it is pulling in a random 6 records from all those available. Heres the code.
is there anything I can do here?

list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'allowSearch' => '0',
'limit' => '6',
'orderBy' => 'RAND()',
));

Re: [zaba] limit to first 6 records not working with order by rand()

By Jason - July 12, 2011

Hi,

What you need to do is return 6 records in their normal order and then randomize them using the shuffle() function.

For example, if you wanted to get the 6 newest news records in a random order, you can do this:

list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'allowSearch' => '0',
'limit' => '6',
'orderBy' => "createdDate DESC",
));

shuffle($newsRecords);


You can now output $newsRecords normally.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] limit to first 6 records not working with order by rand()

By zaba - July 12, 2011

Do you have to put in an orderby clause if you are using the same order as the editor?

Re: [Jason] limit to first 6 records not working with order by rand()

By zaba - July 12, 2011

Thanks,
works perfectly!