Page 1 of 1

2nd set of eyes please - pager logic???

Posted: Sun Nov 04, 2007 10:38 pm
by alex.barylski
I'm reaching the end of my day and before I goto bed I want finish this freaking pager code I've been putting off for over a month...sleep will come to me and it will be all your doing. ;)

The implementation is simple. I'm basically generating a fixed number of page indicies, something like Google does. For this demonstration I use 5 page indicies.

I want to implement a balanced index pager, so for example consider these situations:

1) If there are five pages in total and the user is on page 2 the output will be:

[1][2][3][4][5]

2) If there are ten pages in total and the user is on page 5 the output will be:

[3][4][5][6][7]

From the above two outputs I hope I made it clear what I mean by balanced implementation? I don't want the active page to always be the first page index - except when just starting and there are no previous pages. Basically continuous shifting of the median active page until a balance can be reached on either side. The logic for shifting the active page on either the start or finish is where I think my code is faulted.

There is also a bug which is causing the last page to not show as an index until the second page has been reached.

Here is my current implementation:

Code: Select all

$pages = 5; // Show upto 5 page indicies at once - balanced
$index = $this->getActivePage();
$total = $this->getTotalPages();

//
// Calculate number of indicies to show on left and right hand side of active index - keep balanced
$start  = $index - floor($pages / 2);
$finish = $index + floor($pages / 2);
$start  = ($start < 0 ? 0 : $start);
$finish = ($finish > $total ? $total : $finish);

for($i=$start; $i<$finish; $i++){
	if($i == $index){
		echo '<li style="display: inline">'.($i+1).'</li>';
	}
	else{
		echo '<li style="display: inline"><a href="?cmd'.$this->getCommands().'&idx='.$i.'">'.($i+1).'</a></li>';
	}
}
Any help appreciated as I really could use some sleep tonight :)

Posted: Sun Nov 04, 2007 11:08 pm
by s.dot
Wow, here's a code I wrote a long time ago that does what you're asking. I'm too tired right now to pour through your code.. so maybe you can take a look at mine and see what you're doing wrong?

This code probably isn't optimized, but it works!

Code: Select all

function getPageLinks($num_rows,$per_page,$page,$linktext)
{
	$numpages		= $this->getNumPages($num_rows,$per_page)+1;
	$limit			= $page + 10;
	$prev			= $page - 1;
	$next			= $page + 1;
	$prev_start		= $page - 4;
	$next_start		= $page + 5;
	
	if($prev < 1)				$prev		= 1;
	if($next > $numpages)		$next		= $numpages;
	if($prev_start < 1)			$prev_start	= 1;
	if($next_start > $numpages)	$next_start	= $numpages;
	
	if($prev_start > 2)
	{
		echo '<a href="'.$linktext.'&page=1"><<</a>&nbsp;&nbsp;&nbsp;';
	}
	
	if($page != 1)	echo '<a href="'.$linktext.'&page='.$prev.'">Prev</a> &nbsp;&nbsp;&nbsp;';
	
	for($i=$prev_start;$i<$next_start;$i++)
	{
		if($page != $i)		echo "<a href=\"$linktext&page=$i\">$i</a> ";
		else				echo $i.' ';
	}
	
	if($next < $numpages)		echo "&nbsp;&nbsp;&nbsp;<a href=\"$linktext&page=$next\">Next</a>";
	
	if($next_start < $numpages-1)
	{
		echo '&nbsp;&nbsp;&nbsp;<a href="'.$linktext.'&page=';
		echo $numpages-1;
		echo '">>></a>';
	}
}

Posted: Sun Nov 04, 2007 11:33 pm
by alex.barylski
Hey man, thanks for the reply...

Unfrotunately I don't think your code is doing what I want. It's basically doing what I already have done, but not handling keeping the selected page balanced between previous and next page indicies.

This is the only code I really needed:

Code: Select all

$numpages   = 30;
    $page       = 0;
    $limit      = $page + 10;
    $prev       = $page - 1;
    $next       = $page + 1;
    $prev_start = $page - 4;
    $next_start = $page + 5;

    if($prev < 1)
    	$prev = 1;

    if($next > $numpages)
    	$next = $numpages;

    if($prev_start < 1)
    	$prev_start = 1;

    if($next_start > $numpages)
    	$next_start = $numpages;


    for($i=$prev_start;$i<$next_start;$i++){
			if($page != $i)
				echo "<a href=\"&page=$i\">$i</a>";
			else
				echo $i.' ';
    }
And it doesn't look like there are any calculations being performed other thanbounds checking, which my code already does. :)

I'm looking to meet a slightly more complicated set of rules than bounds checking. Thats why it's such a PITA especially when I just want to goto bed. :(

Posted: Mon Nov 05, 2007 2:17 am
by alex.barylski
Weeeeeeeeee....finally solved the problem - I think. Haven't fully tested yet but it's looking good so far. :)

Cheers :)

Posted: Mon Nov 05, 2007 3:03 am
by s.dot
It's basically doing what I already have done, but not handling keeping the selected page balanced between previous and next page indicies.
It does.

See screenshot!

Image

Page 14 is the selected page, with balanced indices on the left and right.