limit()

limit($offset = null, $limit = null)

Limits and/or Paginate your results by changing the query to bring
only the correct amount or the range.
@param Integer $offset
@param Integer $limit
@return PhpBURN_Core

Params


$offset
This can work in two different ways, if $limit isn’t set so this param will became the limit and will bring the X entries that you ask for if you set $limit param than it will be the offset of your range limit.

$limit
This will define the limit with the offset ( mostly used to paginate your results )

Explanation


This is a VERY VERY VERY helpful function, as you may know each database have it’s own way to do this thing but with PhpBURN you don’t have to worry about that, you just have to use and focus on your own business, pretty nice uh? 😉

Well, let’s see it running:

Limit to 10 results

$user = new Users();

$user->limit(10);
$user->order('id_users', 'DESC');
$user->find();

while($user->fetch()) {
       print_r($user->toArray());
}

This will generate to your database something like that:

MySQL

SELECT 
     .... 
FROM 
     users 
ORDER BY 
     id_users 
LIMIT 
     10

MSSQL (2005+ compatible)

SELECT TOP 10 * ,
     ....
FROM
     dbo.users
ORDER BY
     dbo.users.id_users DESC

Now let’s PAGINATE: limit to 10 results starting from entry 0

$user = new Users();

$user->limit(0,10);
$user->order('id_users', 'DESC');
$user->find();

while($user->fetch()) {
       print_r($user->toArray());
}

This will generate to your database something like that:

MySQL

SELECT 
     .... 
FROM 
     users 
ORDER BY 
     id_users 
LIMIT 
     0,10

MSSQL (2005+ compatible)

SELECT TOP 10 * 
    FROM ( 
        SELECT ROW_NUMBER() OVER (
               ORDER BY dbo.users.id_user DESC
        ) AS _rowNumber_, 
       ..... 
       FROM dbo.users 
) AS _results_ 
WHERE _rowNumber_ > 0

And for each database it will construct the correct way of paginate, look that can be very painful if you have to write every single query when you have to paginate in some databases.