How do I show paginated links in my view?

From PHP on Trax

Jump to: navigation, search

Lets say we have a list of users that we are displaying and want to paginate it.

Example

In order to force Active Record to make the results paginated you have to pass into find_all an array with the key "per_page" or (the 3rd parameter $limit) if either are passed in it will paginate.



<?
find_all
($conditions$order$limit$joins)
find_all("age > 25"null$_REQUEST['per_page'])
or
find_all($params_array)
find_all(array(
    
"per_page" => $_REQUEST['per_page'], 
    
"order" => "last_name DESC")
)
?>

controller code



<?
class UserManagerController extends ApplicationController {
    function 
index() {
        
$user = new User;
                
        
# like this 
        
$this->users $user->find_all(array(
                         
"per_page" => $_REQUEST['per_page'],
                         
"conditions" => "age > 25"));
        
# or like this (one or the other)
        
$this->users $user->find_all("age > 25",
                                       
null,
                                       
$_REQUEST['per_page']);

        
# $this->users is an array of objects not an object.
        # we need a reference to the original object 
        # that did the query $user.
        
$this->paginated_user =& $user;
    }
}
?>

view code index.phtml



<?= pagination_range_text("paginated_user"?><br>
<? foreach($users as $user): ?>
    <?= $user->id.". ".$user->name ?><br>
<? endforeach; ?>
<br>
<?= pagination_limit_select("paginated_user")?> &nbsp; 
<?= pagination_links("paginated_user"?>

This should display out:



Showing 21 - 40 of 100 items.
3. John Doe
34.  Steve Smith
...
[per page select box]   << < 1 2 3 4 5  > >>

View Functions

There are 3 functions available in ActiveRecordHelper to display paging html out: (on all 3 functions $object_name_or_object can be the string name of the paginated object or the actual object itself.)



<?= pagination_range_text($object_name_or_object,  $format "Showing %d - %d of %d items."?>
displays: Showing 21 - 40 of 100 items.

<?= pagination_links($object_name_or_object?>
displays: << < 1 2 3 4 5  > >>

<?= pagination_limit_select($object_name_or_object$default_text "per page:"?>
displays: [per page select box]
Personal tools