Search code examples
phpmysqlpagination

PHP pagination Adjacents


I was able to create a working pagination system for my application. But the problem I am having is when a user does a search, it can/will display over 100 pages in the pagination.

I am trying to figure out how to show only like 5 on each side of the current page. I would like to create a FIRST page button, and a LAST page button, but I'll deal with that later.

So here is the first part of code that counts the records in the database table:

 <?php 
   function countRecords()
   {
    // The application takes a lot of user input, which then builds a query here.
    // The user input goes into a session variable called $_SESSION['where']
    // I'll start with the actual query code
    $sql = "SELECT COUNT(DISTINCT CONTAINER_NUMBER) AS TOTAL 
                   FROM 'contTable'" . " WHERE (" . $_SESSION['where'] . ");";
    $sqlres = @mysql_query($sql) or die();
    $row = mysql_fetch_row($sqlres);
    $return $row[0];
   }

So code above gets the count from the table depending on the search criteria.

Here is the next piece of code that prints the grid. I'll keep it as short as possible:

 function displayrecords()
 {
  $rec_limit = 100;
  $targetpage = "containers.php";
  if(isset($_GET['page']))
  {
   $page = $_GET['page'];
   $offset = $rec_limit * ($page - 1);
  }
  else
  {
   $page = 1;
   $offset = $rec_limit * ($page - 1);
  }
  $left_rec = countRecords() - ($page * $rec_limit);

  $select = "";
  $_SESSION['where'] = "";
  // user input variables are here that build a variable called $_SESSION['where']
  // I'll skip the code down to the query
  if ($_SESSION['where'] != "") $select = "SELECT * FROM 'contTable'" . " WHERE 
      (" . $_SESSION['where'] . ") GROUP BY container, bol LIMIT " . $offset . ",
      " . $rec_limit . ";";     
 }

Please excuse any typos or missing brackets and whatnot. Just know the above code works.

Now here is the code for the rest of the pagination:

 $total_records = countRecords();
 $total_pages = ceil($total_records / $rec_limit);
 $adjacents = 5; // I just added this variable. I don't know what to do with it
 $previousPage = $page - 1;
 $nextPage = $page + 1;
 $querystring = "";

 foreach ($_GET as $key => $value)
 {
   if ($key != "page") $querystring .= "$key=$value&amp;";
 }
 echo '<ul style="border: 0px solid red; margin: 3px;" class="pager">';
 if ($left_rec < $rec_limit) 
 {
   $last = $page - 2;
   echo @"<li><a href=\"$targetpage?page=$previousPage&$querystring\">
   Previous</a></li>";
   for($i = 1; $i <= $total_pages; $i++)
   {
     echo @"<li  " . ((($page+1)==$i)? "class=\"active\"" : "") . ">
     <a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
   }
 }
 else if ($page == 0) 
 {
   for($i = 1; $i <= $total_pages; $i++)
   {
     echo @"<li  " . ((($page+1)==$i)? "class=\"active\"" : "") . ">
      <a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
   }
   echo @"<li><a href=\"$targetpage?page=$nextPage&$querystring\">Next</a></li>";
 }
 else if ($page > 0) 
 {
   $last = $page - 2;
   echo @"<li><a href=\"$targetpage?page=$previousPage&$querystring\">
    Previous</a></li> ";
   for($i = 1; $i <= $total_pages; $i++)
   {
     echo @"<li  " . ((($page+1)==$i)? "class=\"active\"" : "") . ">
      <a href=\"$targetpage?page=$i&$querystring\">$i</a></li>";
   }
   echo @"<li><a href=\"$targetpage?page=$nextPage&$querystring\">Next</a></li>";
   }
 echo '</ul>';  
 }

So, with all of the code above, I can display a grid, and display the pages at the bottom of the application. But I don't want to show all 100 pages, only 5 on each side of the current page. I know I need to utilize the variable called $adjacents and plug it in to the paging portion of the code. But I'm not exactly sure how to do it.

I hope I am being clear on my request.

Please help.


Solution

  • Instead of looping through all of the pages:

    for($i = 1; $i <= $total_pages; $i++) 
    

    Try doing something like this:

    $start = ($page < $adjacents ? 1 : $page - $adjacents);
    $end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
    
    for($i= $start; $i <= $end; $i++)
    //Here you can loop through the numbers within adjacents of the current page