Display pagination links in groups...

My total pages is stored in the variable $total_pages
The current page is stored in the variable: $page
The total records are stored in the variable $total_records

I want to display pagination links but do not want more then 12 links

So for example:

I have 1001 total records with 10 records per page, and say for example I am viewing page 1.

I need the code to show the following

[1] .. [2].. [3].. [4].. [5].. [20]..  [40].. [60].. [80]..  [101]

If I'm on page 10, I would see (always show page 1):

[1]..[8]..[9]..[10]..[11]..[12]..[13]..[20]..[40]..[60]..[80]..[101]

The math is not exact but I think anyone reading this would get the picture (php does the divison and displays at much as it can within a maximum number of pagination links)..

Also, I just need an example (I'm an advanced PHP programmer but this is getting to me ..). The examples I saw online just show like [1] [2] [3] [100]
MarkProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MarkProgrammerAuthor Commented:
Here is the code I am trying:

	public function create_pagination_links($records, $limit, $current_page_num, $rsection, $view='') {
	
		$pages = ceil($records / $limit);
		$page = $current_page_num;
		$current = $page;
		
		$pagebefore = $page-1;
		$pageafter = $page+1;
		
		$output = '<ul class="pagination">';

		if($pagebefore>0) $output .="<li><a href='{$this->create_pagination_link($rsection,$pagebefore,$view)}'>&laquo;</a></li>";
		else $output .= "<li class='disabled'><span>&laquo;</span></li>";
		
		$links = array();

		if ($pages > 3) {
			// this specifies the range of pages we want to show in the middle
			$ind = 2;
			$min = max($current - $ind, $ind);
			$max = min($current + $ind, $pages-1);
			#echo $min.'-'.$max;

			// we always show the first page
			$links[] = "1";

			// we're more than one space away from the beginning, so we need a separator
			if ($min > 2) {
				$links[] = "...";
			}

			// generate the middle numbers
			for ($i=$min; $i<$max+1; $i++) {
				$links[] = "$i";
			}

			// we're more than one space away from the end, so we need a separator
			if ($max < $pages-1) {
				$links[] = "...";
			}
			// we always show the last page
			$links[] = "$pages";
		} 
		
		else {
			$links = array("1", "2", "3");
		}
		
		foreach($links AS $pagelinknum) {

			if($pagelinknum=='...') {
				$output .= "<li><span>...</span></li>";
			}
		
			else if($page==$pagelinknum) { 
				$output .= "<li class='active'><a href='{$this->create_pagination_link($rsection,$pagelinknum,$view)}'>{$pagelinknum}</a></li>";
			}
			
			else {
				$output .= "<li><a href='{$this->create_pagination_link($rsection,$pagelinknum,$view)}'>{$pagelinknum}</a></li>";
			}		
		
		}
		
		if($page<$pages) {
			$output .= "<li><a href='{$this->create_pagination_link($rsection,$pageafter,$view)}'>&raquo;</a>";
		}

		else {
			$output .= "<li class='disabled'><span>&raquo;</span></li>";
		}
		
		$output .= '</ul>';
		
		return $output;

	}
	
	public function create_pagination_link($rsection,$pagenum,$view='') {
		return "?section={$rsection}&page={$pagenum}&view={$view}";	
	}

Open in new window


With 876 records displaying 10 per page, this is the output I am getting:

«123...88»

I would like it to show:

1 2 3 .. 10 .. 30 .. 60 .. 80 .. 100

Equally divided
0
MarkProgrammerAuthor Commented:
Figured it out:

	public function paginationHTML($page, $start_limit, $limit, $records, $URL, $RecordName) {

		$lastPage = ceil($records/$limit);
		$LINKS_PER_STEP = 5;
		
		$startofrecords = $start_limit+1;
		$endofrecords = $start_limit+10;
		if($endofrecords>$records) $endofrecords = $records;
		$showingrecords = "Page $page: Showing $RecordName Records {$startofrecords}-{$endofrecords} Of $records";

		// Nav buttons
		
		$result = '<ul class="pagination pagination-lg">';

		if ($page>1) $result .= "<li><a href='{$URL}1'>&lt;&lt;</a></li><li><a href='{$URL}{$this->ret($page-1)}'>&lt;</a></li>";
		else $result .= "<li class='disabled'><span>&lt;&lt;</span></li><li class='disabled'><span>&lt;</span></li>";

		$result .= "<li><span>&nbsp;&nbsp; $showingrecords &nbsp;&nbsp;</span></li>";
		if ($page<$lastPage) $result .= "<li><a href='{$URL}{$this->ret($page+1)}'>&gt;</a></li><li><a href='{$URL}{$lastPage}'>&gt;&gt;</a></li>";
		else $result .= "<li class='disabled'><span>&gt;</span></li><li class='disabled'><span>&gt;&gt;</span></li>";
		
		$result .= "</ul><br>";

		// Now calculate page links...

		$lastp1 = 1;
		$lastp2 = $page;
		$p1 = 1;
		$p2 = $page;
		$c1 = $LINKS_PER_STEP+1;
		$c2 = $LINKS_PER_STEP+1;
		$s1 = '';
		$s2 = '';
		$step = 1;
		while (true) {
		
			if ($c1>=$c2) {
				$s1 .= $this->paginationGap($lastp1,$p1) . $this->paginationLink($p1,$page,$URL);
				$lastp1 = $p1;
				$p1 += $step;
				$c1--;
			}
			else {
				$s2 = $this->paginationLink($p2,$page,$URL) . $this->paginationGap($p2,$lastp2) . $s2;
				$lastp2 = $p2;
				$p2 -= $step;
				$c2--;
			}
			
			if ($c2==0) {
				$step *= 10;
				$p1 += $step-1;         // Round UP to nearest multiple of $step
				$p1 -= ($p1 % $step);
				$p2 -= ($p2 % $step);   // Round DOWN to nearest multiple of $step
				$c1 = $LINKS_PER_STEP;
				$c2 = $LINKS_PER_STEP;
			}
			
			if ($p1>$p2) {
				$result .= $s1 . $this->paginationGap($lastp1,$lastp2) . $s2;
				if (($lastp2>$page)||($page>=$lastPage)) return "<ul class='pagination'>$result</ul>";
				$lastp1 = $page;
				$lastp2 = $lastPage;
				$p1 = $page+1;
				$p2 = $lastPage;
				$c1 = $LINKS_PER_STEP;
				$c2 = $LINKS_PER_STEP+1;
				$s1 = '';
				$s2 = '';
				$step = 1;
			}
		}
	}
	
		private function paginationLink($pagenumforlink, $currentpage, $URL) {
		  if ($pagenumforlink==$currentpage) return "<li class='active'><a href='{$URL}{$pagenumforlink}'>{$pagenumforlink}</a></li>";
		  return "<li><a href='{$URL}{$pagenumforlink}'>{$pagenumforlink}</a></li>";
		}	
		
		private function paginationGap($p1, $p2) {
			$x = $p2-$p1;
			if ($x==0) return false;
			else if ($x==1) return false;
			else if ($x<=10) return "<li><span> .. </span></li>";
			else if ($x<=100) return "<li><span> ... </span></li>";
			else return "<li><span> .... </span></li>";
		}
	
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.