How to limit results to a range of records

4 posts by 2 authors in: Forums > CMS Builder
Last Post: January 4, 2010   (RSS)

Re: [terryally] How to limit results to a range of records

By Dave - January 1, 2010

Hi Terry,

Are you using getRecords() or a direct mysql query?

If you are using getRecords() you could use this option to return 3 records:
'limit' => 3,

There's another option called 'offset' if you want to get a specific record, such as:
'offset' => 3,
'limit' => 1,

This does the same as LIMIT 3,1 in straight mysql

However, I'd recommend getting all 3 in one query and then doing something like this:

list($record1, $record2, $record3) = $records;

Which will put each record in it's own variable.

Hope that helps!
Dave Edis - Senior Developer

interactivetools.com

Re: [Dave] How to limit results to a range of records

By terryally - January 2, 2010 - edited: January 2, 2010

Hello Dave,

The offset worked brilliantly and the speed of the query was just as good.

I also want to test your recommended option with variables but I am not sure how to using this with getRecords() - or is this with a straight mysql query?

Happy New Year to you 'n staff.

Terry

Re: [terryally] How to limit results to a range of records

By Dave - January 4, 2010

Hi Terry,

Great, glad to hear it's working.

If you have it working now I wouldn't change it but when you have an array (or list) of records and you want to access them individually you can do so with the list() function in PHP. See: http://php.net/list

So if you had some viewer code like this:
// load matching records
list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'limit' => '3',
...


You could loop over $newsRecords with foreach() or if you wanted to access them individually you could assign them to variables like this:

list($news1, $news2, $news3) = $newsRecords;

This is sometimes helpful when you want to display records in different places on the page. You could then display record 2's title with <?php echo $news2['title'] ?>.

It can sometimes be an alternative to having to have multiple queries to get individual records. But like I said, if you have it working it's probably safe to leave it (or at least backup your working copy before you experiment!).

Hope that helps!
Dave Edis - Senior Developer

interactivetools.com