title: php array pagination with mid range numbers
body: <p>I have an array pagination class. But that class displays all the page numbers. </p>
  
  <p>I mean if i have 100 pages, it displays all 100 page numbers. </p>
  
  <p>Can someone help me to implement mid range numbers in this pagination class</p>
  
  <pre><code>&lt;?php
  
    class pagination
    {
  
      /**
       * Properties array
       * @var array   
       * @access private 
       */
      private $_properties = array();
  
      /**
       * Default configurations
       * @var array  
       * @access public 
       */
      public $_defaults = array(
        'page' =&gt; 1,
        'perPage' =&gt; 10 
      );
  
      /**
       * Constructor
       * 
       * @param array $array   Array of results to be paginated
       * @param int   $curPage The current page interger that should used
       * @param int   $perPage The amount of items that should be show per page
       * @return void    
       * @access public  
       */
      public function __construct($array, $curPage = null, $perPage = null)
      {
        $this-&gt;array   = $array;
        $this-&gt;curPage = ($curPage == null ? $this-&gt;defaults['page']    : $curPage);
        $this-&gt;perPage = ($perPage == null ? $this-&gt;defaults['perPage'] : $perPage);
      }
  
      /**
       * Global setter
       * 
       * Utilises the properties array
       * 
       * @param string $name  The name of the property to set
       * @param string $value The value that the property is assigned
       * @return void    
       * @access public  
       */
      public function __set($name, $value) 
      { 
        $this-&gt;_properties[$name] = $value;
      } 
  
      /**
       * Global getter
       * 
       * Takes a param from the properties array if it exists
       * 
       * @param string $name The name of the property to get
       * @return mixed Either the property from the internal
       * properties array or false if isn't set
       * @access public  
       */
      public function __get($name)
      {
        if (array_key_exists($name, $this-&gt;_properties)) {
          return $this-&gt;_properties[$name];
        }
        return false;
      }
  
      /**
       * Set the show first and last configuration
       * 
       * This will enable the "&lt;&lt; first" and "last &gt;&gt;" style
       * links
       * 
       * @param boolean $showFirstAndLast True to show, false to hide.
       * @return void    
       * @access public  
       */
      public function setShowFirstAndLast($showFirstAndLast)
      {
          $this-&gt;_showFirstAndLast = $showFirstAndLast;
      }
  
      /**
       * Set the main seperator character
       * 
       * By default this will implode an empty string
       * 
       * @param string $mainSeperator The seperator between the page numbers
       * @return void    
       * @access public  
       */
      public function setMainSeperator($mainSeperator)
      {
        $this-&gt;mainSeperator = $mainSeperator;
      }
  
      /**
       * Get the result portion from the provided array 
       * 
       * @return array Reduced array with correct calculated offset 
       * @access public 
       */
      public function getResults()
      {
        // Assign the page variable
        if (empty($this-&gt;curPage) !== false) {
          $this-&gt;page = $this-&gt;curPage; // using the get method
        } else {
          $this-&gt;page = 1; // if we don't have a page number then assume we are on the first page
        }
  
        // Take the length of the array
        $this-&gt;length = count($this-&gt;array);
  
        // Get the number of pages
        $this-&gt;pages = ceil($this-&gt;length / $this-&gt;perPage);
  
        // Calculate the starting point 
        $this-&gt;start = ceil(($this-&gt;page - 1) * $this-&gt;perPage);
  
        // return the portion of results
        return array_slice($this-&gt;array, $this-&gt;start, $this-&gt;perPage);
      }
  
      /**
       * Get the html links for the generated page offset
       * 
       * @param array $params A list of parameters (probably get/post) to
       * pass around with each request
       * @return mixed  Return description (if any) ...
       * @access public 
       */
      public function getLinks($params = array())
      {
        // Initiate the links array
        $plinks = array();
        $links = array();
        $slinks = array();
  
        // Concatenate the get variables to add to the page numbering string
        $queryUrl = '';
        if (!empty($params) === true) {
          unset($params['page']);
          $queryUrl = '&amp;amp;'.http_build_query($params);
        }
  
        // If we have more then one pages
        if (($this-&gt;pages) &gt; 1) {
          // Assign the 'previous page' link into the array if we are not on the first page
          if ($this-&gt;page != 1) {
            if ($this-&gt;_showFirstAndLast) {
              $plinks[] = ' &lt;a href="?page=1'.$queryUrl.'"&gt;&amp;laquo;&amp;laquo; First &lt;/a&gt; ';
            }
            $plinks[] = ' &lt;a href="?page='.($this-&gt;page - 1).$queryUrl.'"&gt;&amp;laquo; Prev&lt;/a&gt; ';
          }
  
          // Assign all the page numbers &amp; links to the array
          for ($j = 1; $j &lt; ($this-&gt;pages + 1); $j++) {
            if ($this-&gt;page == $j) {
              $links[] = ' &lt;a class="selected"&gt;'.$j.'&lt;/a&gt; '; // If we are on the same page as the current item
            } else {
              $links[] = ' &lt;a href="?page='.$j.$queryUrl.'"&gt;'.$j.'&lt;/a&gt; '; // add the link to the array
            }
          }
  
          // Assign the 'next page' if we are not on the last page
          if ($this-&gt;page &lt; $this-&gt;pages) {
            $slinks[] = ' &lt;a href="?page='.($this-&gt;page + 1).$queryUrl.'"&gt; Next &amp;raquo; &lt;/a&gt; ';
            if ($this-&gt;_showFirstAndLast) {
              $slinks[] = ' &lt;a href="?page='.($this-&gt;pages).$queryUrl.'"&gt; Last &amp;raquo;&amp;raquo; &lt;/a&gt; ';
            }
          }
  
          // Push the array into a string using any some glue
          return implode(' ', $plinks).implode($this-&gt;mainSeperator, $links).implode(' ', $slinks);
        }
        return;
      }
    }
  </code></pre>
  
  <h2>Update:</h2>
  
  <p>Here is how i'm using</p>
  
  <pre><code>$contacts = array();
  if(count($contacts))
      { 
        $pagination = new pagination($contacts,  (isset($_GET['page']) ? $_GET['page'] : 1), 50);
        $ContactPages = $pagination-&gt;getResults();
    if (count($ContactPages) != 0) {
          echo $pageNumbers = $pagination-&gt;getLinks();
      }
  }
  </code></pre>