topical media & game development

talk show tell print

professional-search-14-seophp-include-simple-pager.inc.php / php



  <?php
  
  // generates pager links
  class SimplePager
  {
    var _rows;
    var _limit;
    var _function_callback;
    var _page_number_parameter_index;
    var _max_listed_pages;
    var _previous_prompt;
    var _next_prompt;
    var _show_disabled_links;
    
    // constructor
    function SimplePager(rows, limit, function_callback, 
                         page_number_parameter_index = 1)
    {
      this->_rows = rows;
      this->_limit = limit;
      this->_function_callback = function_callback;
      this->_page_number_parameter_index = page_number_parameter_index;
      this->_max_listed_pages = 10;
      this->_previous_prompt = '<< back';
      this->_next_prompt = 'next >>';
    }
  
    // displays the pager links
    function display(page_number = 1, &rows, additional_parameters = '')
    {
      offset = (page_number - 1) * this->_limit;
      row_count = sizeof(this->_rows);        
      total_pages = ceil(row_count / this->_limit);    
      rows = array_slice(this->_rows, offset, this->_limit);
      
      // will contain the pager links
      links = array();
  
      // display the "<< back" link
      if (page_number > 1) 
      { 
        prev_page = page_number - 1;
        links[] = 
          "<a href='" . 
          call_user_func_array(this->_function_callback, 
            array_merge(additional_parameters, 
            array(this->_page_number_parameter_index => prev_page))) . 
          "'>this->_previous_prompt</a>";
      } 
      else
      {
        // no "<< back" link if the visitor is on the first page
        links[] = this->_previous_prompt;
      }    
      
      // calculate the first and last listed pages
      start = floor(page_number / (this->_max_listed_pages)) 
               * this->_max_listed_pages;    
      if (!start) start = 1;
      end = (total_pages < start + this->_max_listed_pages - 1) ? 
             total_pages : start + this->_max_listed_pages - 1;
      
      // display pager links
      for (i = start; i <= end; i++) 
      {
        // display links for all pages except the current one
        if (i != page_number) 
        {
          links[] = 
            "<a href='" . 
            call_user_func_array(this->_function_callback, 
              array_merge(additional_parameters, 
              array(this->_page_number_parameter_index => i))) . 
            "'>i</a>";  
        } 
        else 
        {
          // no link for the current page
          links[] = i;          
        }
      }
  
      // display "next >>" link
      if (page_number < total_pages) 
      { 
        next_page = page_number + 1;
        links[] = 
          "<a href='" . 
          call_user_func_array(this->_function_callback, 
            array_merge(additional_parameters, 
            array(this->_page_number_parameter_index => next_page))) . 
          "'>this->_next_prompt</a>";
      } 
      else  
      {
        // no "next >>" link if the visitor is on the last page    
        links[] = this->_next_prompt;    
      }
      
      // return the pager text  
      return implode(' | ', links);
    } 
  }
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.