Jump to content


Photo

Pagination


  • Please log in to reply
13 replies to this topic

#1 mcmuney

mcmuney
  • Members
  • PipPipPip
  • Advanced Member
  • 358 posts

Posted 04 August 2006 - 01:43 AM

I'm using the below code for basic pagination; however, I'd like something more advanced, something like this: http://www.sphere.co...edrop=1&from=10 and I'm clueless as where to begin. Thanks in advance.

<?
class Pager  
	   {  
	       function getPagerData($numHits, $limit, $page)  
	       {  
		   $numHits  = (int) $numHits;  
		   $limit    = max((int) $limit, 1);  
		   $page     = (int) $page;  
		   $numPages = ceil($numHits / $limit);  
	
		   $page = max($page, 1);  
		   $page = min($page, $numPages);  
	
			if ($page==0)
			   $offset = ($page) * $limit;  
			   else
			   $offset = ($page - 1) * $limit;  
			   
	
		   $ret = new stdClass;  
	
		   $ret->offset   = $offset;  
		   $ret->limit    = $limit;  
		   $ret->numPages = $numPages;  
		   $ret->page     = $page;  
	
		   return $ret;  
	       } 
       function getPageingLine($PageURL,$PageNo,$numPages,$frmname="form1",$class="dark_blue"){
		//For MultiLinagual
		$strFirst = "First";
		$strNext = "Next";
		$strLast = "Last";
		$strPrevious = "Previous";
		if ($PageNo == 1) // this is the first page - there is no previous page 
			echo $strFirst . " | ";
		else
			echo "<a  onclick=\"javascript:".$frmname.".page.value=1;".$frmname.".formSubmit.click();\" href=\"$PageURL&page=1\" class=$class>".$strFirst."</a> | ";

		if ($PageNo == 1) 
			echo $strPrevious;  
		else             
			echo "<a onclick=\"javascript:".$frmname.".page.value=".($PageNo-1).";".$frmname.".formSubmit.click();\" href=\"$PageURL&page=" . ($PageNo - 1) . "\" class=$class>".$strPrevious."</a>";  
	
		if ($PageNo == $numPages) 
			echo " | " . $strNext;  
		else            
			echo " | <a onclick=\"javascript:".$frmname.".page.value=".($PageNo+1).";".$frmname.".formSubmit.click();\" href=\"$PageURL&page=" . ($PageNo + 1) . "\" class=$class> ".$strNext."</a>";
	
		if ($PageNo == $numPages) 
			echo " | ". $strLast;  
		else 
			echo " | <a onclick=\"javascript:".$frmname.".page.value=".$numPages.";".$frmname.".formSubmit.click();\" href=\"$PageURL&page=" . $numPages . "\" class=$class> ".$strLast."</a>";	
       }

				function getPageingLineForComment($PageURL,$PageNo,$numPages,$class="dark_blue"){
		//For MultiLinagual
		$strFirst = "First";
		$strNext = "Next";
		$strLast = "Last";
		$strPrevious = "Previous";
		if ($PageNo == 1) // this is the first page - there is no previous page 
			echo $strFirst . " | ";
		else
			echo "<a href=\"$PageURL&page=1#comment_s\" class=$class>".$strFirst."</a> | ";

		if ($PageNo == 1) 
			echo $strPrevious;  
		else             
			echo "<a href=\"$PageURL&page=" . ($PageNo - 1) . "#comment_s\" class=$class>".$strPrevious."</a>";  
	
		if ($PageNo == $numPages) 
			echo " | " . $strNext;  
		else            
			echo " | <a href=\"$PageURL&page=" . ($PageNo + 1) . "#comment_s\" class=$class> ".$strNext."</a>";
	
		if ($PageNo == $numPages) 
			echo " | ". $strLast;  
		else 
			echo " | <a href=\"$PageURL&page=" . $numPages . "#comment_s\" class=$class> ".$strLast."</a><br>";	
       }		   
	   
	   }?>


#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 04 August 2006 - 01:45 AM

I'd like something more advanced


Perhaps you can be a little more vague?

Seriously though, what exactly do you want to change about the behavior your script, cause "more advanced" doesn't mean anything.

#3 mcmuney

mcmuney
  • Members
  • PipPipPip
  • Advanced Member
  • 358 posts

Posted 04 August 2006 - 05:58 PM

What do you mean it's vague. I posted the link to what I meant by more advanced. Click on the link and look at their pagination. Thanks.

#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 04 August 2006 - 06:16 PM

I have loooked at that link, there is nothing special about their pagination.

It has a previous link, followed by links to the pages, followed by a link to the next page.  What is advanced about that?

#5 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 04 August 2006 - 06:45 PM

When posting to a help forum, I would highly recommend that you be as specific as possible on the problem you are having or the functionality you are looking for. This will ensure that people will quickly have the information they need to provide you assistance.

It's not wise to make others have to dig around to discern what your question is.

Onto your question, after looking deeper the pagination on the page you link, I think I believe what type of functionality you are looking for.

You would like your pagiation to have a sort of "result page" limit, showing only a certain number of links to other pages around your current page. For examle, say you're on page 50 of 100, you want your links to look something like:

<< Prev  47 48 49 50 51 52 53  Next >>

To accomplish this, you'll want to set something of a "maxlinks" variable.

Let me ponder on this for a moment, and I"ll post back with some sample code.





#6 mcmuney

mcmuney
  • Members
  • PipPipPip
  • Advanced Member
  • 358 posts

Posted 04 August 2006 - 07:00 PM

Ok, sorry about that. I'll wait for your sample code posts.

My current code is basically showing "previous" and "next" and my definition of advanced was the example (link) that shows "previous" 1 2 3 (boxed) and "next".

The current code is used within several pages as an "include" file.

Thanks again.

#7 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 04 August 2006 - 07:06 PM

So you want to show links to all pages, or something like I mentioned?

#8 mcmuney

mcmuney
  • Members
  • PipPipPip
  • Advanced Member
  • 358 posts

Posted 04 August 2006 - 07:09 PM

Yes, something like what you mentioned, showing 10 (Previous 11 12..20 Next ). I guess the boxes are more part of the design and not function. Thanks.

#9 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 04 August 2006 - 09:29 PM

Okay, here we go. I updated your getPageingLine function for the pagination you requested, but you can add this to your other paging functions as well if you want similar functionality.

function getPageingLine($PageURL,$PageNo,$numPages,$frmname="form1",$class="dark_blue"){

  // Set a variable to limit how many links are shown
  $maxLinks = 10;

  // Cut our max links in half, so we can get an even number of
  // page links before and after the current page
  $linksPerSide = floor($maxLinks / 2);

  // Determine the start and end pages, set variables accordingly
  // Start by checking if we need to limit our results. We determine this
  // by checking if there are more results pages than max links
  if($numPages < $maxLinks){
	// We have less pages than max links, so show them all
	$startPage = 1;
	$endPage = $numPages;
  } else {
	// If we have more results than max links can show at one time, then we need
	// to limit what links are printed.
	//
	// Start by checking if we are currently less than half of our max links away
  	// from the first page of results
  	if(($PageNo - $linksPerSide) <= 1){  
		// If we are less than half our max links to the first page,
		// set our first link to print out as page 1
		$startPage = 1;

		// Add the number of max links to the start page to get the end page
		$endPage = $startPage + $maxLinks;
	} elseif(($PageNo + $linksPerSide) >= $numPages){
		// If we are less than half our max links to the last page,
		// set our last page as the number of total pages
		$endPage = $numPages;

		// Subtract the number of max links from the end page to get the start page
		$startPage = $endPage - $maxLinks;
	} else {
		// If we have more than half max links between the first and last pages of results
		// set our current page in the middle of the links we show
		$startPage = $PageNo - $linksPerSide;

		$endPage = $PageNo + $linksPerSide;
	}
  }
       
  //For MultiLinagual
  $strFirst = "First";
  $strNext = "Next";
  $strLast = "Last";
  $strPrevious = "Previous";
  if ($PageNo == 1) // this is the first page - there is no previous page 
   echo $strFirst . " | ";
  else
   echo "<a  onclick=\"javascript:".$frmname.".page.value=1;".$frmname.".formSubmit.click();\" href=\"$PageURL&page=1\" class=$class>".$strFirst."</a> | "; 

  if ($PageNo == 1) 
   echo $strPrevious;  
  else             
   echo "<a onclick=\"javascript:".$frmname.".page.value=".($PageNo-1).";".$frmname.".formSubmit.click();\" href=\"$PageURL&page=" . ($PageNo - 1) . "\" class=$class>".$strPrevious."</a>";  


  // Here we will print out our page links
  for($i = $startPage;$i <= $endPage;$i++){
	// If we are printing out the current page number, don't make it a links
	if($i == $PageNo){
		echo $i;
	} else {
		// Otherwise, print a link for the page	
		echo " <a onclick=\"javascript:".$frmname.".page.value=".($i).";".$frmname.".formSubmit.click();\" href=\"$PageURL&page=" . ($i) . "\" class=$class> ".$i."</a>";
	}
  }


  if ($PageNo == $numPages) 
   echo " | " . $strNext;  
  else            
   echo " | <a onclick=\"javascript:".$frmname.".page.value=".($PageNo+1).";".$frmname.".formSubmit.click();\" href=\"$PageURL&page=" . ($PageNo + 1) . "\" class=$class> ".$strNext."</a>"; 
 
  if ($PageNo == $numPages) 
   echo " | ". $strLast;  
  else 
   echo " | <a onclick=\"javascript:".$frmname.".page.value=".$numPages.";".$frmname.".formSubmit.click();\" href=\"$PageURL&page=" . $numPages . "\" class=$class> ".$strLast."</a>";  
       }

Regarding the boxes that you see on the link you provided, that's all formatting. You can change the look of your pagination however you wise, and the function will still be the same.

Let me know how this works for you.

#10 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 04 August 2006 - 09:59 PM

There are also at least two wonderful tutorials on the PHPFreaks site that accomplish exactly what you ask for.  I would recommend checking them out to learn a thing or two so repeating this in the future doesn't consist of trying to dig up old code.  Good luck!
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#11 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 04 August 2006 - 10:20 PM

There are some great tutorials on this site for basic pagination, however mcmuney wanted something a little more advanced.

#12 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 04 August 2006 - 10:30 PM

There's one titled Advanced Pagination Tutorial or something like that which limits the pages displayed on both sides of the current page, just like he wanted.  Either way, you have a solution, either in that tutorial or in the code posted above.  They're both worth checking out, just to see the different methods of acomplishing it.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#13 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 04 August 2006 - 10:49 PM

nethnet, I looked for an advanced tutorial, but I was unable to locate it. Normally those are the first things I link to when replying to code questions.

Do you have a link to that tutorial? I would be ineterested to see the method that was used.

#14 mcmuney

mcmuney
  • Members
  • PipPipPip
  • Advanced Member
  • 358 posts

Posted 06 August 2006 - 12:10 AM

Thanks a bunch, that was exactly what I was looking for. Now I'm off to figure out the design/formatting aspect. Thanks again!!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users