Solved

Display pagination links in groups...

Posted on 2014-10-18
2
101 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

862 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now