title: PHP Pagination Display
body: <p>I'm using bootstrap pagination, and a combination PHP/mysql method of displaying my results, (ex. results.php?results=1 for each page).  This is working fine, but actually displaying the pagination div at the bottom of my page, i.e. which one is active, whether or not you can click the following or preceding items (if there actually are results on those pages)</p>
  
  <p>So I've done a specific case for just about every combination I could think of that would affect anything, but it is certainly not efficient.  Can anyone suggest a better way to do it?  Some of the cases will be obvious, like if I'm on page one, I don't want to show page 0 and -1 in the pagination, others are less obvious, like if I'm on page 3, and there are no more pages, I want page 3 to be in the middle, as there are 5 numbers displayed, but you should not be able to click on 4 and 5.  </p>
  
  <p>I'm using an exact replica of the plugin showed <a href="http://getbootstrap.com/components/#pagination" rel="nofollow">here</a> except I've added left and right increase / decrease by one in addition to the first / last page.  </p>
  
  <pre><code>    &lt;div id="pagination" style="width: 340px; margin-left: auto; margin-right: auto;"&gt;
  &lt;? if ($results == 1) {   ?&gt;    
  &lt;ul class="pagination"&gt;
    &lt;li class="disabled"&gt;&lt;a href="#"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li class="disabled"&gt;&lt;a href="#"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li class="active"&gt;&lt;a href="#"&gt;&lt;? echo $results; ?&gt; &lt;span class="sr-only"&gt;(current)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; $num_res) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 1); ?&gt;"&gt;&lt;? echo $results + 1; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 2)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 2); ?&gt;"&gt;&lt;? echo $results + 2; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 3); ?&gt;"&gt;&lt;? echo $results + 3; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 4)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 4); ?&gt;"&gt;&lt;? echo $results + 4; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($last_page); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
    &lt;? } ?&gt;
    &lt;? if ($results == 2) { ?&gt;    
  &lt;ul class="pagination"&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".(1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 1); ?&gt;"&gt;&lt;? echo $results - 1; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li class="active"&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results); ?&gt;"&gt;&lt;? echo $results; ?&gt; &lt;span class="sr-only"&gt;(current)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 2)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 1); ?&gt;"&gt;&lt;? echo $results + 1; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 2); ?&gt;"&gt;&lt;? echo $results + 2; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 3); ?&gt;"&gt;&lt;? echo $results + 3; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($last_page); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
    &lt;? } ?&gt;
    &lt;? if (($results == 3) &amp;&amp; (($last_page == 3))) { ?&gt;   
  &lt;ul class="pagination"&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".(1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 2); ?&gt;"&gt;&lt;? echo $results - 2; ?&gt; &lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 1); ?&gt;"&gt;&lt;? echo $results - 1; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li class="active"&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results); ?&gt;"&gt;&lt;? echo $results; ?&gt;&lt;span class="sr-only"&gt;(current)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 1); ?&gt;"&gt;&lt;? echo $results + 1; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 2); ?&gt;"&gt;&lt;? echo $results + 2; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($last_page); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
    &lt;? } ?&gt;
      &lt;? if (($results &gt; 2) &amp;&amp; ($results &lt; ($last_page - 1))) { ?&gt;    
  &lt;ul class="pagination"&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".(1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 2); ?&gt;"&gt;&lt;? echo $results - 2; ?&gt; &lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 1); ?&gt;"&gt;&lt;? echo $results - 1; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li class="active"&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results); ?&gt;"&gt;&lt;? echo $results; ?&gt;&lt;span class="sr-only"&gt;(current)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 1); ?&gt;"&gt;&lt;? echo $results + 1; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 2); ?&gt;"&gt;&lt;? echo $results + 2; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($last_page); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
    &lt;? } ?&gt;
      &lt;? if (($results &gt; 2) &amp;&amp; ($results == ($last_page - 1))) { ?&gt;   
  &lt;ul class="pagination"&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".(1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 3); ?&gt;"&gt;&lt;? echo $results - 3; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 2); ?&gt;"&gt;&lt;? echo $results - 2; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 1); ?&gt;"&gt;&lt;? echo $results - 1; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li class="active"&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results ); ?&gt;"&gt;&lt;? echo $results ?&gt;&lt;span class="sr-only"&gt;(current)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 1); ?&gt;"&gt;&lt;? echo $results + 1; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results + 1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li &lt;?if ($num_rows &lt; ($num_res * 3)) { echo "class=\"disabled\""; } ?&gt;&gt;&lt;a href="&lt;? echo $url."&amp;results=".($last_page); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
    &lt;? } ?&gt;
     &lt;? if (($results &gt; 2) &amp;&amp; ($results == $last_page) &amp;&amp; ($results != 3)){ ?&gt;    
  &lt;ul class="pagination"&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".(1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 1); ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 4); ?&gt;"&gt;&lt;? echo $results - 4; ?&gt; &lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 3); ?&gt;"&gt;&lt;? echo $results - 3; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 2); ?&gt;"&gt;&lt;? echo $results - 2; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results - 1); ?&gt;"&gt;&lt;? echo $results - 1; ?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li class="active"&gt;&lt;a href="&lt;? echo $url."&amp;results=".($results ); ?&gt;"&gt;&lt;? echo $results ?&gt;&lt;span class="sr-only"&gt;(current)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li class="disabled"&gt;&lt;a href="#"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
    &lt;li class="disabled"&gt;&lt;a href="#"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
    &lt;? } ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>If no one feels like helping with the first portion, I understand, it works as it is as far as I can tell.</p>
  
  <p>But I also am wondering if there's a way that on my <code>class="disabled"</code>, I can remove the hrefs?  Or do I just have to put an if statement on every one?  Thanks.</p>
  
  <p>Edit: Based on @Scopey's recommendations.  I changed a few things as I saw fit, as well as a couple variable names.  </p>
  
  <pre><code>$numberOfPages = ceil($num_rows / $num_res);
  $numberOfPages = (int)$numberOfPages;
  
  $url = explode('&amp;', $_SERVER['REQUEST_URI']);
  function myFilter($string) {
  return strpos($string, 'results=') === false;
  }
  
  $url = array_filter($url, 'myFilter');
  $url = implode('&amp;', $url);
  
  ?&gt; 
  &lt;ul class="pagination"&gt;
       &lt;li&lt;?php if($results === $numberOfPages): ?&gt; class="disabled"&lt;?php endif; ?&gt;&gt;&lt;a href="&lt;?php
  if($results !== $numberOfPages){
      echo $url.'&amp;results=' . 1;
  } else { echo '#'; } 
  ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
      &lt;li&lt;?php if($results === 1): ?&gt; class="disabled"&lt;?php endif; ?&gt;&gt;&lt;a href="&lt;?php
  if($results !== 1){
      echo $url.'&amp;results=' . ($results - 1);
  } else { echo '#'; } 
  
  ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-left"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
  
  &lt;?php
  
  // Print the pagination...
  // Minimum of 5 pages in the pagination, even if there aren't 5 pages...
  $pageCount = ($numberOfPages &lt; 5) ? 5 : $numberOfPages;
  
  // Loop through from page 1 until the last page ($pageCount)
  for($i = 1; $i &lt;= $pageCount; $i++)
  {
       // Echo out just the beginning of the &lt;li&gt; tag as we don't yet
       // know if this needs to be disabled...
       echo '&lt;li';
       // Added to show if current page is active
       if($i === $results) {
            echo ' class="active"';
       }
       // Check if:
       //  - This current page is greater than the amount of pages of
       //    results we have
       //  - OR, this is the currently selected page
       if($i &gt; $numberOfPages || $results === $i)
       {
            // &lt;li&gt; tag needs the disabled class....
            echo ' class="disabled"';
       }
       // Finish the &lt;li&gt; tag and start generating the link
       echo '&gt;&lt;a href="';
       // Opposite logic from above... Only if this is a page we know about
       // AND this is not the current page.
       if($i &lt; $numberOfPages &amp;&amp; $results !== $i)
       {
            // The link will be your page, with the get param, and the
            // current page number we're printing
            echo $url.'&amp;results=' . $i;
       }
       else
       {
            // Otherwise just a # (no link)
            echo '#';
       }
       // Finish this page... Print the page number ($i)
       echo '"&gt;' . $i . '&lt;/a&gt;&lt;/li&gt;';
  }
  ?&gt;
      &lt;li&lt;?php if($results === $numberOfPages): ?&gt; class="disabled"&lt;?php endif; ?&gt;&gt;&lt;a href="&lt;?php
  if($results !== $numberOfPages){
      echo $url.'&amp;results=' . ($results + 1);
  } else { echo '#'; } 
  ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
   &lt;li&lt;?php if($results === $numberOfPages): ?&gt; class="disabled"&lt;?php endif; ?&gt;&gt;&lt;a href="&lt;?php
  if($results !== $numberOfPages){
      echo $url.'&amp;results=' . $numberOfPages;
  } else { echo '#'; } 
  ?&gt;"&gt;&amp;nbsp;&lt;i class="fa fa-lg fa-angle-double-right"&gt;&lt;/i&gt;&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
  
  &lt;?php
  </code></pre>
  
  <p>@Scobey, this works great, as intended, and same as my original post, except for a couple things.  In my example, I was able to get the current page in the middle of the .  For example,  &lt;&lt; 1 | 2 | <strong>3</strong> | 4 | 5 >>, that's why I had so many different statements with +1 and -1, +2, -2 etc.  </p>
  
  <p>Second, my intention was to display 5 page options at all times, not just a minimum of five.  So if I was on page 6, I would only see &lt;&lt; 4 | 5 | <strong>6</strong> | 7 | 8 >>, not 1-8.  It may not be necessary to do it this way, it's a nice to have.  But, I don't want to get to a point where I have over 50 different page numbers in the pagination.</p>