phppaginationredbean

Best way to do paging using redbeanphp?


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 ');


Solution

  • 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);