Welcome Guest, Not a member yet? Register   Sign In
Pagination with page number as uri rather than offset
#1

[eluser]crmdesignstudio[/eluser]
Hi all,

I searched the forums but can't seem to find an answer for this one. Does anyone know how to change the pagination class to make the links add the page number into the url rather than the offset?

Ex. If the per page is 10 and offset is 10, rather than having the url be 'foo/10' for the second page of results I would like it to be 'foo/2' and my model will calculate the offset.

Thanks in advance!
CM
#2

[eluser]jblack199[/eluser]
Well, while I havent used the pagination class with CI I have used pagination in other things as well as written custom pagination classes for use without any MVC or template engines.

I've taken a quick look at the pagination class, and first thing you'd have to change it:

var $per_page = 10; // Max number of items you want shown per page

would need to be in your controller before the query itself..

2nd thing in your controller you'd need to SELECT COUNT(*) FROM tbl WHERE to get the total count... once you get the count you'd need to get the total pages... CI pagination class does this through use of:

// Calculate the total number of pages
$num_pages = ceil($this->total_rows / $this->per_page);

in the create_links function... you could use the prebuilt functionality to do it just fine really...

I imagine you'd then change this:

Code:
if ($n == '' && $this->first_url != '')
                        {
                            $output .= $this->num_tag_open.'<a >anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
                        }
                        else
                        {
                            $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;

                            $output .= $this->num_tag_open.'<a >anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
                        }

to this
Code:
if ($n == '' && $this->first_url != '')
                        {
                            $output .= $this->num_tag_open.'<a >anchor_class.'href="'.$loop.'">'.$loop.'</a>'.$this->num_tag_close;
                        }
                        else
                        {
                            $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;

                            $output .= $this->num_tag_open.'<a >anchor_class.'href="'.$this->base_url.$loop.'">'.$loop.'</a>'.$this->num_tag_close;
                        }

doing that, would at least get the URL's to look correctly... the math involved in finding the offset would be up to you to figure out...
#3

[eluser]crmdesignstudio[/eluser]
@jblack199 thanks for pointing me in the right direction! After customizing a few other variables in the library all pagination links are working successfully!

Thanks again,
CM




Theme © iAndrew 2016 - Forum software by © MyBB