Jump to content

help with pagination display


scarhand

Recommended Posts

heres my code:

 

<?php

if ($pagi_count > 1)
{
	echo '<ul class="pagination">';
	echo '<li><a href="#">«</a></li>';

	$pagi_start = 1;
	$pagi_end = $pagi_count;

	for ($p = $pagi_start; $p <= $pagi_end; $p++)
	{
		$link = "$url/page/".$get_letter;

		if ($p > 1)
			$link .= "/$p";

		if ($p == $pagi_num)
			$class = ' class="current"';
		else
			$class = '';

		echo '<li'.$class.'><a href="'.$link.'">'.$p.'</a></li>';
	}

	echo '<li><a href="#">»</a></li>';
	echo '</ul>';
}

?>

 

now this works fine, however i want it to only show 20 page numbers, and if there are more than 20 total pages, i want the current page to be in the middle of the list (unless of course the current page is 1 or 2 then it would be more to the left of the list........

 

example:

 

with 13 total pages, on page 8:

<< 1 2 3 4 5 6 7 (8) 9 10 11 12 13 >>

 

with 55 total pages, on page 2:

<< 1 (2) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 >>

 

with 65 total pages, on page 12:

<< 2 3 4 5 6 7 8 9 10 11 (12) 13 14 15 16 17 18 19 20 21 >>

 

im really stumped on the math for this....any help would be much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/226695-help-with-pagination-display/
Share on other sites

i got it

 

<?php

if ($pagi_count > 1)
{
	echo '<ul class="pagination">';

	if ($pagi_num > 1)
		echo "<li><a href=\"$url/page/$get_letter\">«</a></li>";

	$pagi_start = 1;
	$pagi_end = $pagi_count;

	if ($pagi_count > 15) 
	{
		$pagi_start = $pagi_num - 7;
		$pagi_end = $pagi_num + 7;

		if ($pagi_start < 1) 
		{
			$pagi_end -= $pagi_start;
			$pagi_start = 1;
		}

		if ($pagi_end > $pagi_count) 
		{
			 $pagi_start += ($pagi_count - $pagi_end);
			 $pagi_end = $pagi_count;
		}
	}

	for ($p = $pagi_start; $p <= $pagi_end; $p++)
	{
		$link = "$url/page/$get_letter";

		if ($p > 1)
			$link .= "/$p";

		if ($p == $pagi_num)
			$class = ' class="current"';
		else
			$class = '';

		echo '<li'.$class.'><a href="'.$link.'">'.$p.'</a></li>';
	}

	if ($pagi_num < $pagi_count)
		echo "<li><a href=\"$url/page/$get_letter/$pagi_count\">»</a></li>";

	echo '</ul>';
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.