We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: search pagination

Development and Programming

BridgingUnit's avatar
BridgingUnit
214 posts
16 years ago
BridgingUnit's avatar BridgingUnit

May I introduce my first plugin, BU Search Pagination, built to scratch an itch on a project that’s coming to an introduction forum near you soon. This has been lying around for a little while, largely because I’ve been too busy to attend to it properly, but I see that it’s time to officially release it into the world and find out what people make of it. A few have already had a chance to play with it, but now it’s time for you too.

Add this into your template when you wish to have greater control over customising the pagination tags on your search results page.

The plugin bases the output of its pagination on the search results (per page limit) and result_page parameters set in your search form tag and the total number of results calculated by the Search Module.

It is, as said, my first plugin, so perhaps a bit rough and ready. Let me know what you think and if there’s anything I can do to improve it for you.

Still to do - add in EE standard pagination_links form too

You can use it one of 2 ways.

As a tag pair with variables. Or as a single tag supplying it with a parameter. The former is probably the most useful way. The latter is retained as it was the first way I built it, so figured I’d leave it available anyway.

The tag pair:

{exp:bu_search_pagination}{/exp:bu_search_pagination}

Variables available within:

{current_page}
{total_pages}
{prev_page_path}
{next_page_path}
{first_page_path}
{last_page_path}

As a single tag with the ‘type’ parameter set to display the current_page number:

{exp:bu_search_pagination type='current_page'}

All the variants:

{exp:bu_search_pagination type='current_page'}
{exp:bu_search_pagination type='total_pages'}
{exp:bu_search_pagination type='prev_page_path'}
{exp:bu_search_pagination type='next_page_path'}
{exp:bu_search_pagination type='first_page_path'}
{exp:bu_search_pagination type='last_page_path'}

Example usage, using the tag pair form:

{exp:bu_search_pagination}
{if prev_page_path}
 <a href="http://{path={prev_page_path}}">Prev</a>
{/if}

Page {current_page} of {total_pages} 

{if next_page_path}
 <a href="http://{path={next_page_path}}">Next</a>
{/if}
{/exp:bu_search_pagination}

Thanks to d3i and particularly Masuga for suggestions and requests that have encouraged development.

Latest version available for download from the BU Search Pagination page on my website.

Hope you find it useful.

       
Sue Crocker's avatar
Sue Crocker
26,054 posts
16 years ago
Sue Crocker's avatar Sue Crocker

Happy Friday. Saw your tweet. Will install and test later today. Thanks for contributing to the community.

       
BridgingUnit's avatar
BridgingUnit
214 posts
16 years ago
BridgingUnit's avatar BridgingUnit

Cheers Sue. No problem - try to do what I can when I can 😊

       
BridgingUnit's avatar
BridgingUnit
214 posts
16 years ago
BridgingUnit's avatar BridgingUnit

Just added a couple of new variables in response to 1) in this thread so you can get reference to the results on the page too:

{page_results_first}

and

{page_results_last}

.

e.g. on the 2nd page where you have 5 results per page, page_results_first would be 6, page_results_last would be 10, so you can display ‘results 6 - 10’ with this code:

{exp:bu_search_pagination}
results {page_results_first} - {page_results_last}
{/exp:bu_search_pagination}

Hopefully get to 2) pagination_links in a day or three

       
BridgingUnit's avatar
BridgingUnit
214 posts
16 years ago
BridgingUnit's avatar BridgingUnit

Version 1.2.0 sees a new pair variable added so that you can output and style pagination links for search result pages.

{pagination_pages}
{/pagination_pages}

This has 2 parameters available to it -

format

and

limit

pagination_pages has the following single variables available within it

{count}
{page_number}
{page_path}
count

tells you what position you are on in the pagination_pages loop.

page_number

tells you the page number for the current result page processed.

page_path

provides a link path for the current result page processed.

The ‘format’ parameter for pagination_pages allows you to change the pagination so that it is split up into blocks of pages. By default each block contains up to 5 sets of result pages. The ‘limit’ parameter allows you to change that.

For example, if you had 19 pages of results and a ‘limit’ of 4, pages 1-4 would be the first block, 5-8 the second and so on. The final block would have 3 pages.

If you’ve specified a ‘format’ parameter of ‘block’ then the following single variables also become available within pagination_pages:

{total_block_pages}
{prev_block_path}
{prev_block_pages}
{next_block_path}
{next_block_pages}
total_block_pages

just reflects the number of blocks that the results have been split into. If you don’t specify a ‘limit’ this will be 5 as that’s the default limit.

prev_block_path

provides the link back to the first page within the previous block.

prev_block_pages

provides a reference for the pages contained within the previous block. This follows the format 1 - 4, 5 - 8 etc. Neither of these two variables will be available on pages within the first block.

next_block_path

provides the link to the first page within the next block.

next_block_pages

provides a reference for the pages contained within the next block. This follows the format 4 - 8, 9 - 12 etc. Neither of these two variables will be available on pages within the last block.

Here’s an example using the pagination_pages pair variable:

{exp:bu_search_pagination}
{pagination_pages format='block' limit='10'}
{if prev_block_path!='' AND count==1}
<a href="http://{path={prev_block_path}}">{prev_block_pages}</a>
{/if}

{if page_number==current_page}
{page_number}
{if:else} 
<a href="http://{path={page_path}}">{page_number}</a> 
{/if}

{if next_block_path!='' AND count==total_block_pages}
<a href="http://{path={next_block_path}}">{next_block_pages}</a>
{/if}
{/pagination_pages}
{/exp:bu_search_pagination}
       
Ryan M.'s avatar
Ryan M.
1,511 posts
16 years ago
Ryan M.'s avatar Ryan M.

For the record, this plugin is fantastic, and blows the default search pagination out of the water.

       
BridgingUnit's avatar
BridgingUnit
214 posts
16 years ago
BridgingUnit's avatar BridgingUnit

Cheers Ryan.

Had a thought that perhaps from a user’s point of view

prev_block_path

may be better if it were to go to the last page in the previous block rather than the first…. I’ll add that as an option when I get a moment.

       
lizlux's avatar
lizlux
10 posts
16 years ago
lizlux's avatar lizlux

Thanks for this! Have you been able to make this work while displaying tag search results from the solspace {exp:tag:search_results} module? Right now my search results are displaying, then my tag results, then the pagination links, as opposed to cycling through all of the search results and then listing the tags results on the last page. Any hints? Thanks!

       
VRDL's avatar
VRDL
23 posts
16 years ago
VRDL's avatar VRDL

Is it possible to show the pagination like the following?

1 2 3 4 5

20 21 22 23 24

I couldn’t find it on the documentationpage, but it would be a great addition 😊

       
jamesa's avatar
jamesa
75 posts
16 years ago
jamesa's avatar jamesa

brilliant plugin this, just used it on the last bit of a site i have been working on. saved me so much time, thanks!

       
bb6600's avatar
bb6600
13 posts
15 years ago
bb6600's avatar bb6600

Awesome plugin!

For some reason I can’t seem to be able to use the

{exp:search:total_results}

tag inside it though. It just spits out some garbage like “M20o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr” instead of the actual number. Any thoughts on this or how can I get the total results number without going the PHP route?

Basically, I am trying to show something along these lines: “Showing 1-10 out of 27 results”

       
BridgingUnit's avatar
BridgingUnit
214 posts
15 years ago
BridgingUnit's avatar BridgingUnit

@bb6600 - Looks like it’s muddling things up. Have you tried separating the two things out so that they’re not nested? e.g.

{exp:bu_search_pagination}
Showing {page_results_first} - {page_results_last}
{/exp:bu_search_pagination}

{exp:search:total_results} 
out of {total_results} results
{/exp:search:total_results}

Something like that?

@lizlux Sorry for missing your post - think I’ve become unsubscribed from this thread. I’ve not tried this plugin yet in combination with Solspace’s Tags module. I’ll try and replicate when I get a chance to use Tags and Search together. If you’ve come up with any solutions, please post them here.

@Verdel Automatisering Again, sorry for missing you. Looks like you were trying to show pages links. The code posted above for pagination_pages should cover you.

@jamesa Thanks - glad it’s been useful.

       
bb6600's avatar
bb6600
13 posts
15 years ago
bb6600's avatar bb6600

Thanks again BU!

Just in case someone else is interested in this, I’ll post my complete code for generating a pagination area that looks something like this:

showing 1-10 out of 44 results prev 1 2 3 4 next

{exp:bu_search_pagination}showing <strong>{page_results_first} - {page_results_last}</strong>{/exp:bu_search_pagination} out of <strong>{exp:search:total_results} result{if "{exp:search:total_results}" != 1}s{/if}</strong>

{exp:bu_search_pagination}<ul>
    <li>< a href="{path={prev_page_path}}"{if prev_page_path}{if:else} class="inactive"{/if}>prev</a></li>
    {pagination_pages}
        {if page_number == current_page}<li><a href="http://{path={page_path}}" class="active">{page_number}</a></li>
        {if:else}<li><a href="http://{path={page_path}}">{page_number}</a></li>{/if}
    {/pagination_pages}
    <li>< a href="{path={next_page_path}}"{if next_page_path}{if:else} class="inactive"{/if}>next</a></li>
</ul>{/exp:bu_search_pagination}

Remove the extra space before the next/prev ‘a’ tags.

It’s complete with CSS classes to highlight the current page and inactive next/prev links.

       
BridgingUnit's avatar
BridgingUnit
214 posts
15 years ago
BridgingUnit's avatar BridgingUnit

No problem. And thanks for posting your code.

       
Cyborg's avatar
Cyborg
22 posts
15 years ago
Cyborg's avatar Cyborg

Any plans on releasing this for EE2.0?

       
1 2

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.