Jump to content

Archived

This topic is now archived and is closed to further replies.

mcmuney

Pagination

Recommended Posts

I'm using the below code for basic pagination; however, I'd like something more advanced, something like this: [url=http://www.sphere.com/search?q=test&lang=en&datedrop=1&from=10]http://www.sphere.com/search?q=test&lang=en&datedrop=1&from=10[/url] and I'm clueless as where to begin. Thanks in advance.

[code]
<?
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>";
      }  
 
  }?>
[/code]

Share this post


Link to post
Share on other sites
[quote]I'd like something more advanced[/quote]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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:

[b]<< Prev  47 48 49[/b] 50 [b]51 52 53  Next >>[/b]

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.



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
So you want to show links to [b]all[/b] pages, or something like I mentioned?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Okay, here we go. I updated your [b]getPageingLine[/b] function for the pagination you requested, but you can add this to your other paging functions as well if you want similar functionality.

[code=php:0]
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>"; 
      }
[/code]

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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
There are some great tutorials on this site for basic pagination, however [b]mcmuney[/b] wanted something a little more advanced.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[b]nethnet[/b], 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.

Share this post


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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.