Welcome Guest, Not a member yet? Register   Sign In
URI safe pagination patch
#1

[eluser]kylehase[/eluser]
This is patch to the pagination class that aims to provide pagination without breaking other URI data. For instance if your URI is something like:

example.com/class/method/search/phone/sort/price or
example.com/class/method/filter/online

The page links will preserve the extra URI fields:

example.com/class/method/20/search/phone/sort/price or
example.com/class/method/40/filter/online etc.

Your class must be able to parse the URI with or without the page number.
See http://ellislab.com/forums/viewthread/63531/ for help with that.

Code:
--- Pagination.orig.php 2007-10-30 22:17:10.000000000 +0900
+++ Pagination.php      2007-10-30 22:19:23.000000000 +0900
@@ -115,8 +115,15 @@
                        return '';
                }

-               // Determine the current page number.
-               $CI =& get_instance();
+               $CI =& get_instance();
+               // Preserve existing URI if exists
+               if(is_numeric($CI->uri->segment($this->uri_segment))){
+                        $start_segment = $this->uri_segment +1;
+                        $extended_uri = ($CI->uri->segment($start_segment))?$CI->uri->uri_to_assoc($start_segment):'';
+               }
+               else $extended_uri = ($CI->uri->segment($this->uri_segment))?$CI->uri->uri_to_assoc($this->uri_segment):'';
+               $extended_uri = ($extended_uri)?'/'.$CI->uri->assoc_to_uri($extended_uri):'';
+                // Determine the current page number.
                if ($CI->uri->segment($this->uri_segment) != 0)
                {
                        $this->cur_page = $CI->uri->segment($this->uri_segment);
@@ -154,15 +161,15 @@
                // Render the "First" link
                if  ($this->cur_page > $this->num_links)
                {
-                       $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
+                       $output .= $this->first_tag_open.'<a href="'.$this->base_url.$extended_uri.'">'.$this->first_link.'</a>'.$this->first_tag_close;
+
                }
-
                // Render the "previous" link
                if  (($this->cur_page - $this->num_links) >= 0)
                {
                        $i = $uri_page_number - $this->per_page;
                        if ($i == 0) $i = '';
-                       $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
+                       $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.$extended_uri.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
                }

                // Write the digit links
@@ -179,7 +186,7 @@
                                else
                                {
                                        $n = ($i == 0) ? '' : $i;
-                                       $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
+                                       $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.$extended_uri.'">'.$loop.'</a>'.$this->num_tag_close;
                                }
                        }
                }
@@ -187,14 +194,14 @@
                // Render the "next" link
                if ($this->cur_page < $num_pages)
                {
-                       $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
+                       $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).$extended_uri.'">'.$this->next_link.'</a>'.$this->next_tag_close;
                }

                // Render the "Last" link
                if (($this->cur_page + $this->num_links) < $num_pages)
                {
                        $i = (($num_pages * $this->per_page) - $this->per_page);
-                       $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
+                       $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.$extended_uri.'">'.$this->last_link.'</a>'.$this->last_tag_close;
                }

                // Kill double slashes.  Note: Sometimes we can end up with a double slash




Theme © iAndrew 2016 - Forum software by © MyBB