[SOLVED] Restricting the number of links shown

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

[SOLVED] Restricting the number of links shown

Post by Rovas »

I made a couple of month ago a PHP page which implements pagination: then as I do now I put the problems encountered on the forum. At that I didn' t needed to put a restriction on the links to the different pages resulted from the pagination but now I see I have to.
I' m trying to display something like
if you are on page 3: 1 2 3 4 5 6
if you are on page 5: 3 4 5 6 7 8
Here is the part of the code that outputs the links

Code: Select all

/*
                                      $Current  indicates the current page
                                       $i. $j, $l the indicators for the positions
                                      $totalN the total number of pages                             
                               */
                              
                               $j=$i+3; 
                               $l = $Current- 2; 
				do
				{
					if($i!=$Current && $i<$j  && $l<0)
					{echo "<a href='categ.php?cat=" .$_GET['cat']  ."&page=" .$i ."'>" .$i."</a>";}
                                          // displaying the first two links i.e. 3 , 4  
                                        else if ($l>0) 
                                        { 
                                                for ($t=$l; $t<$Current; $t++)
                                                   {echo "<a href='categ.php?cat=" .$_GET['cat']  ."&page=" .$l ." '>" .$i."</a>";} 
                                       }
					else if ($i==$Current){echo "<b>" .$i ."</b>";}
					$i=$i+1;
				}
				while ($i<=$totalN || $i< $j);

The following code shows from the current page to the last page.
Any ideas where I screw up or any ideas for a nicer display (links ) ?
Last edited by Rovas on Thu Feb 22, 2007 12:32 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why are there two loops? You really only need one, the for loop, with some added logic inside of it for the current page.

There's also $Curent and $Current.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

I reedited to remove the spelling and sintax mistakes. But I don' t understand
You really only need one, the for loop
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Get a piece of paper and write out exactly what is happening, statement-by-statement, for several values with the code you've posted. .. or alter the echoes such that they differ so you can see which one is being used.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

I solved it (I' am not totally satisfied with the solution but it works). Thanks feyd for help (too tired yesterday to understand what you meant). The ideea is simple think of number intervals from mathematics and from there is a piece of cake:

Code: Select all

$a= $Current-2;
  if ($a<=0) {$a=1;}
  switch ($totalN- $Current)
  case 0: $b=1; break;
  case 1: $b=2; break;
  case 2: $b=2; break;
  for($i=$a; $i<=$b: $i++)
  { 
     // similar to the former do.. while
  }
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I wrote a new paging class the other day for OoerTV. It does this limiting thing.

Code: Select all

	class Pager {

		var $total;
		var $page;
		
		var $recordsPerPage = 10;
		var $paddingDigits = 1;
		var $seperator = " ";
		var $showStart = TRUE;
		var $showEnd = TRUE;
		var $endSeperator = "&hellip;";
		
		var $_arrPager = array();

		function Pager($total,$page,$url) {
		
			$this->total=$total;
			$this->page=$page;
			$this->url=$url;
			if (strpos($this->url,"?")!==FALSE) { $this->url.="&"; }

		}
		
		function setRecordsPerPage($recordsPerPage) {
			$this->recordsPerPage = $recordsPerPage;
		}
		
		function setPaddingDigits($paddingDigits) {
			$this->paddingDigits = $paddingDigits;
		}
		
		function setShowStart($showStart) {
			$this->showStart = $showStart;
		}

		function setShowEnd($showEnd) {
			$this->showEnd = $showEnd;
		}

		function setSeperator($seperator) {
			$this->seperator = $seperator;
		}
		
		function setEndSeperator($endSeperator) {
			$this->endSeperator = $endSeperator;
		}
		
		function displayPager() {

			$pages = ceil($this->total/$this->recordsPerPage);
			
			if ($this->page-$this->paddingDigits<=0) {
				$s = 1;
			} elseif ($this->page+($this->paddingDigits*2)-1>$pages) {
				$s = $pages-($this->paddingDigits*2);
			} else {
				$s = $this->page-$this->paddingDigits;
			}
			
			if ($this->page-($this->paddingDigits*2)+1<=0) {
				$e = ($this->paddingDigits*2)+1;
			} elseif ($this->page+($this->paddingDigits*2)-1>$pages) {
				$e = $pages;
			} else {
				$e = $this->page+$this->paddingDigits;
			}

			if ($s<1) { $s = 1; }
			if ($e>$pages) { $e = $pages; }

			for ($x=$s;$x<=$e;$x++) {
				$page = ($this->page==$x) ? $x : "<a href=\"".$this->url."page=".$x."\">".$x."</a>";
				$this->_arrPager[] = $page;
			}
			
			if ($this->showStart and $this->page > ($this->paddingDigits+1)) {
				array_unshift($this->_arrPager,"<a href=\"".$this->url."page=1\">1</a>",$this->endSeperator);
			}

			if ($this->showEnd and $this->page <= ($pages-($this->paddingDigits+1))) {
				array_push($this->_arrPager,$this->endSeperator,"<a href=\"".$this->url."page=".$pages."\">".$pages."</a>");
			}

			if (is_array($this->_arrPager) and count($this->_arrPager)>1) {
				return implode($this->seperator,$this->_arrPager);
			}

		}
		
	}
It's not actually finished yet, but it should give you an idea.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Thank you but I have made it' s enough: I made some modifications since the last post and it' working
very good (it 's similar the forum when there a lot of pages > 20). Anybody can take our code and modify to suit their needs.
[EDIT]Of course the code written onion2k is better.[/EDIT]
Post Reply