Jump to content

Pagination


mcmuney

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]
Link to comment
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.



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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.