Pagination of search results

Hello! I am trying to find out how to do pagination for search results. I am under the impression that elgg has some built in support for pagination already, but am not sure how to leverage it and use it in search results.

I have an object of subtype 'sport'. When I search for the name of the sport, I'd like to get as a result, a list of users that play that sport. Currently, I have defined a custom view for the 'sport' subtype. The view calls a function that gets a list of users that have a relation to that sport. Then the view loops through each user and displays them in a list. This works well, but I'd like support for pagination.

Is there a way to, having a list of users, display say, 25 of them, and then have pagination to view the rest? My constraints are that it needs to be able to support a large user base. I have no idea how many users this search will return; could be 2, could be 1000.

Thanks in advance!

-Justin

  • There is a view navigation/pagination that can be used to generate pagination controls

    It's built in to the lists, when you use any of the elgg_list_entities* functions pagination is defaulted.  In the getter functions you should set the limit and offset based off the get_input('limit') and get_input('offset')

  • Thanks for the reply.

    So If I understand this right I have two options; 1) use elgg_list_entities_from_relationship(), or 2) make a call to the 'navigation/pagination' view in the view defined for the object. And I'd have to make note of all the @uses at the top of the file to make sure I'm passing in all the necessary data.

    Am I right, or am I over/underthinking it?

    Thanks!

    -Justin

  • That is exactly right.

    Note that the pagination is fairly simple, it takes $_GET parameters for 'limit' and 'offset' so it doesn't work too well if you have multiple pagination controls per page.  If that's the case each pagination takes the same limit and offset which leads to unexpected behavior.  If that's what you're looking at you may want to either ajaxify the pagination for your search results, or show the first batch and link to the full results that you can display in its own page with pagination.

  • Thank you very much! I decided to use elgg_list_entities_from_relationship, and it works just the way I want it to.

    Just a bit of documentation out in public for those that need it:

    in the $options variable for the query, set 'pagination' => true. Also, the 'limit' option controls how many results per page. (eg if 'limit' => 2, there will be 2 results per page)

    Thanks Matt!

  • For multiple pagination controls you can pass a custom 'offset_key' value in the options

  • Oh right, I forgot about the custom offset key.  That's the easy way to deal with multiple pagination controls per page!

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking