Show
Ignore:
Timestamp:
02/25/2008 01:40:41 PM (11 months ago)
Author:
Geert
Message:

Added $_GET support for Pagination.

Works similar as uri_segment. Just set your $_GET pagination key to query_string.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/libraries/Pagination.php

    r2143 r2167  
    1717        protected $style          = 'classic'; 
    1818        protected $uri_segment    = 3; 
     19        protected $query_string   = ''; 
    1920        protected $items_per_page = 10; 
    2021        protected $total_items    = 0; 
     
    7071                $this->directory = trim($this->directory, '/').'/'; 
    7172 
    72                 // Start building a generic URL 
    73                 $this->url = ($this->base_url == '') ? Router::$segments : explode('/', trim($this->base_url, '/')); 
     73                // Build generic URL with page in query string 
     74                if ($this->query_string !== '') 
     75                { 
     76                        // Unvalidated current page 
     77                        $this->current_page = (isset($_GET[$this->query_string])) ? (int) $_GET[$this->query_string] : 1; 
    7478 
    75                 // Convert uri 'label' to corresponding integer if needed 
    76                 if (is_string($this->uri_segment)) 
    77                 { 
    78                         if (($key = array_search($this->uri_segment, $this->url)) === FALSE) 
    79                         { 
    80                                 // If uri 'label' is not found, auto add it to base_url 
    81                                 $this->url[] = $this->uri_segment; 
    82                                 $this->uri_segment = count($this->url) + 1; 
    83                         } 
    84                         else 
    85                         { 
    86                                 $this->uri_segment = $key + 2; 
    87                         } 
     79                        // Insert {page} placeholder 
     80                        $_GET[$this->query_string] = '{page}'; 
     81 
     82                        // Create full URL 
     83                        $this->url = url::site(Router::$current_uri).'?'.str_replace('%7Bpage%7D', '{page}', http_build_query($_GET)); 
    8884                } 
    8985 
    90                 // Create a generic URL with query string and {page} number placeholder 
    91                 $this->url[$this->uri_segment - 1] = '{page}'; 
    92                 $this->url = url::site(implode('/', $this->url)).Router::$query_string; 
     86                // Build generic URL with page as URI segment 
     87                else 
     88                { 
     89                        // Use current URI if no base_url set 
     90                        $this->url = ($this->base_url == '') ? Router::$segments : explode('/', trim($this->base_url, '/')); 
     91 
     92                        // Convert uri 'label' to corresponding integer if needed 
     93                        if (is_string($this->uri_segment)) 
     94                        { 
     95                                if (($key = array_search($this->uri_segment, $this->url)) === FALSE) 
     96                                { 
     97                                        // If uri 'label' is not found, auto add it to base_url 
     98                                        $this->url[] = $this->uri_segment; 
     99                                        $this->uri_segment = count($this->url) + 1; 
     100                                } 
     101                                else 
     102                                { 
     103                                        $this->uri_segment = $key + 2; 
     104                                } 
     105                        } 
     106 
     107                        // Insert {page} placeholder 
     108                        $this->url[$this->uri_segment - 1] = '{page}'; 
     109 
     110                        // Create full URL 
     111                        $this->url = url::site(implode('/', $this->url)).Router::$query_string; 
     112 
     113                        // Unvalidated current page 
     114                        $this->current_page = URI::instance()->segment($this->uri_segment); 
     115                } 
    93116 
    94117                // Core pagination values 
     
    96119                $this->items_per_page     = (int) max(1, $this->items_per_page); 
    97120                $this->total_pages        = (int) ceil($this->total_items / $this->items_per_page); 
    98                 $this->current_page       = (int) min(max(1, URI::instance()->segment($this->uri_segment)), max(1, $this->total_pages)); 
     121                $this->current_page       = (int) min(max(1, $this->current_page), max(1, $this->total_pages)); 
    99122                $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items); 
    100123                $this->current_last_item  = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);