List page - page links/navigation

8 posts by 4 authors in: Forums > CMS Builder
Last Post: July 6, 2009   (RSS)

Hi,

On a list page, w have the navigation between the pages at the bottom (i.e. << previous page, page 1, page 2, next page >> etc etc).

Is there anyway to limit the number of page X links there are, for example, going from:

<< prev - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 next >>

to

<<prev - 1 - 2 - 3 - 4 - 5 next >>

The code for the page numbers is as follows:

<?php foreach (range(1,$productsMetaData['totalPages']) as $page): ?>
<?php echo $page; ?>
<?php endforeach; ?>


If there is a way to limit the foreach in the above, so you just get 1,2,3,4,5...

The main issue would then be that when you are on page 10 (with page links limited to 5), instead of showing the first 5 pages, it needs to show 5 pages around 10...

i.e.

<<prev 8,9,10,11,12 next >>


You see this idea quite alot around on various sites, for example:

http://www.readitswapit.co.uk/TheLibrary.aspx?CategoryID=-1&CategoryName=All+Fiction&PageID=21&TimeSlot=633685482000003118&BookType=999

You see the page nav has 10 either side of where you are.

Any ideas?

Many thanks in advance
Rob

Re: [rjbathgate] List page - page links/navigation

By Dave - January 26, 2009

Hi Rob,

Try something like this:

<?php foreach (range(1,$productsMetaData['totalPages']) as $page): ?>
<?php $maxPageRange = 5; ?>
<?php if ($page > ($productsMetaData['totalPages'] + $maxPageRange)) { continue; } ?>
<?php if ($page < ($productsMetaData['totalPages'] - $maxPageRange)) { continue; } ?>

<?php echo $page; ?>
<?php endforeach; ?>


Let me know if that works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] List page - page links/navigation

Hi,

Thanks Dave, seems like it's almost there, but it's just showing the last 6 page numbers, rather than either side of what page you're on.

So when on page 3, it's showing the last 6 pages (say 40,41,42,43,44,45,46), instead of 1,2,3,4,5,6.

I will email you the URL for a demo.

Thanks
Rob

Re: [rjbathgate] List page - page links/navigation

By Dave - January 27, 2009

Rob,

Ahh, ok. We need the current page number. Display the variables in $productsMetaData like this:

<xmp><?php print_r($productsMetaData); ?></xmp>

Then find the one that has the current page number and replace $productsMetaData['totalPages'] with it in this code:

<?php if ($page > ($productsMetaData['totalPages'] + $maxPageRange)) { continue; } ?>
<?php if ($page < ($productsMetaData['totalPages'] - $maxPageRange)) { continue; } ?>

I think it's called $productsMetaData['page']

Let me know if that works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] List page - page links/navigation

Hi Dave,

Yes, it is 'page'

This code works a treat:

<?php foreach (range(1,$productsMetaData['totalPages']) as $page): ?>

<?php $maxPageRange = 4; ?>
<?php if ($page > ($productsMetaData['page'] + $maxPageRange)) { continue; } ?>
<?php if ($page < ($productsMetaData['page'] - $maxPageRange)) { continue; } ?>

<?php echo $page; ?>

<?php endforeach; ?>


This gives you 4 pages either side of what page you are on.

Thanks Dave!
Rob

Re: [rjbathgate] List page - page links/navigation

By Dave - January 29, 2009

Great, glad to hear it's working! :)
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] List page - page links/navigation

By HDLLC - July 4, 2009

Thanks for the code example!

One question though... I tried that exact snippet, of course changing the variable name to my own. I get the numbers to display, but no links. And, as I progress to page 3, 4, and then five - using the already available next/previous - the number of pages at the bottom simply increases by one, or decreases by one.
So, if I start with page 1, I have 1-5 at the bottom (but no links).
If I go to page 2 - I get 1-6, and page three give me 1-7.

How can I limit this to just a page range of say 5 or so pages, and have each page link so that someone can jump to page 5, or a few pages ahead instead of just crawling through next/previous?

Thanks in advance!

--Jeff