Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Display pagination links in groups...

Posted on 2014-10-18
2
Medium Priority
?
114 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
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to count occurrences of each item in an array.
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

564 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