Help with pagination code

5 posts by 2 authors in: Forums > CMS Builder
Last Post: June 5, 2012   (RSS)

By weblm - June 4, 2012

Was wondering if anyone can help expand this pagination code. Basically.....I've cobbled together bits and pieces from these forums to modify the pagination code. My current code is:

<!-- START PAGINATION CODE -->
<div class="pagelinks">
<?php if ($press_roomMetaData['prevPage']): ?>
<a href="<?php echo $press_roomMetaData['prevPageLink'] ?>"><img src="/images/arrow-left.png" border="0" /> prev</a> <a href="?<?php echo http_build_query(array_merge($_REQUEST, array( 'page' => '1' ))) ?> ">...</a>
<?php else: ?>
&nbsp;
<?php endif ?>

<?php
if (@!$_GET['page']): $current_page = "1";
else: $current_page = $_GET['page'];
endif; ?>

<?php
$startNumber = max($current_page - 3, 1);
$endNumber = min($current_page + 3, $press_roomMetaData['totalPages']);
?>

<?php foreach (range($startNumber,$endNumber) as $page): ?>

<?php if ($page == $current_page): ?>
<strong>[<?php echo $page; ?>]</strong>
<?php else: ?>
<a href="?<?php echo http_build_query(array_merge($_REQUEST, array( 'page' => $page ))) ?>"><?php echo $page; ?></a>
<?php endif ?>

<?php endforeach; ?>

<?php if ($press_roomMetaData['nextPage']): ?>
<a href="?<?php echo http_build_query(array_merge($_REQUEST, array( 'page' => $press_roomMetaData['totalPages'] ))) ?> ">...</a> <a href="<?php echo $press_roomMetaData['nextPageLink'] ?>">next <img src="/images/arrow-right.png" border="0" /></a>
<?php else: ?>
&nbsp;
<?php endif ?>
</div>
<!-- /END PAGINATION CODE -->



What the code does is.....

If you are on page 1.....it shows:
[1] 2 3 4 ... next

If you are on any page in the middle it shows:
prev ... 1 2 3 [4] 5 6 7 ... next

If you are on the last page it shows:
prev ... 16 17 18 [19]

It shows links to the 3 pages on either side of the current page.

-----------------

Basically what I wanted was to give people a way to get to the last page and back to the first page. The only way I could figure this out was adding the ... when you are on the right place.

What I'd like to do is show the actual FIRST and LAST page number.

So if you are on page 1 it would show (where 30 is the last page):
[1] 2 3 4 ... 30 next

If you are on a middle page it would show:
prev 1 ... 12 13 14 [15] 16 17 18 ... 30 next

If you are on the last page it would show:
prev 1 ... 16 17 18 [19]


Now the issue I'm having is how to tell the script how to do this. Basically I don't want the 1 or 30 showing double if you are close to either end. So if I'm on page 3......what I end up getting is:
prev 1 ... 1 2 [3] 4 5 6 ... next


See the double 1's. I need to figure out how to tell the script that if you ware within X pages of the beginning or end.....don't double up the number.

Want a working example of the functionality I want....visit the Apple forums: https://discussions.apple.com/thread/2808065?start=0&tstart=0

Anyone know a good way to accomplish this?

Thanks!

-Kevin
LM

Re: [kblm] Help with pagination code

By Jason - June 5, 2012 - edited: June 5, 2012

Hi Kevin,

<?php if ($press_roomMetaData['nextPage']): ?>
<?php if ($press_roomMetaData['totalPages'] > $endNumber): ?>
<a href="?<?php echo http_build_query(array_merge($_REQUEST, array( 'page' => $press_roomMetaData['totalPages'] ))) ?> "><?php echo $press_roomMetaDate['totalPages'];?></a>
<?php endif ?>

<a href="<?php echo $press_roomMetaData['nextPageLink'] ?>">next <img src="/images/arrow-right.png" border="0" /></a>
<?php else: ?>
&nbsp;
<?php endif ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Help with pagination code

By weblm - June 5, 2012

Hi Jason,

Thanks for the code. I'm getting this error:

Notice: Undefined index: lastPage in articlepage.php on line 205

Any idea why?

-Kevin
LM

Re: [kblm] Help with pagination code

By Jason - June 5, 2012

Hi Kevin,

Looks like I made a "not enough coffee" error. :) I've updated my post with code that should take care of the issue.

Thanks,
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/