?
Solved

Display pagination links in groups...

Posted on 2014-10-18
2
Medium Priority
?
112 Views
Last Modified: 2014-10-18
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]
0
Comment
Question by:Mark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Author Comment

by:Mark
ID: 40389411
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
 

Accepted Solution

by:
Mark earned 0 total points
ID: 40389509
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question