Question

I started using it recently, and did not like the way I did. Wanted suggestions...

I am using like example:

$all = R::findAll('needle', ' ORDER BY title LIMIT 2 ');

Was it helpful?

Solution 2

First off, that won't do anything for pagination. You need to be passing a page and possibly amount to the server to paginate. Other than that, you are doing it right.

$page=1;
$limit=10;
$all=R::findAll('needle','ORDER BY title LIMIT '.(($page-1)*$limit).', '.$limit);

Your other option is to select them all and then return only the portion the user is to view, but this is a bad idea if you have thousands of records:

$needles=R::findAll('needle', 'ORDER BY title');
$page=1;
$limit=10;
$all=array_slice($needles,(($page-1)*$limit),$limit,true);

To find total pages:

$needles=R::count('needle');
$totalPages=ceil($needles/$limit);

OTHER TIPS

I think the proposed solution is okay except that you should use parameter binding otherwise this could cause an SQL injection:

$page=1;
$limit=10;
$all=R::findAll('needle',
'ORDER BY title LIMIT ?,? ',array((($page-1)*$limit),$limit));

Better to use a PHP package like https://github.com/yidas/php-pagination

Because it will also give you the HTML widget to make the pagination links in the bottom; besides the helper to frame the SQL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top