Welcome Guest, Not a member yet? Register   Sign In
Pagination & optional uri parameters
#1

[eluser]Adrian Walls[/eluser]
Hi,

I have a method in my site to browse products by any combination of the following 3 criteria: category, brand or range. Any combination of these including none of them can be included when browsing. For example:

http://mysite.com/our-products/browse
http://mysite.com/our-products/browse/brand/sonia
http://mysite.com/our-products/browse/ca...rand/sonia
http://mysite.com/our-products/browse/ca...nge/dallas
http://mysite.com/our-products/browse/range/dallas
etc etc..

I am converting the uri segements into an array via

Code:
$this->uri->uri_to_assoc(3)

My issue however is when it comes to pagination it always adds the offset to the end:

http://mysite.com/our-products/browse/ca.../dallas/10

Therefore using uri_to_assoc means that the forth array entry has a key of 10 but no value. And when I build up the URL again for the pagination link I need to convert the uri segments array back into a url so I use assoc_to_uri however this then gives me

http://mysite.com/our-products/browse/ca...llas/10/20
http://mysite.com/our-products/browse/ca...s/10/20/30
http://mysite.com/our-products/browse/ca...0/20/30/40
etc etc.

Any ideas how to paginate this way?

Full controller method is

Code:
// load required libraries
        $this->load->library("pagination");
        
        //initalise search data array
        $uri_search_data = $this->uri->uri_to_assoc(3);

        //set status
        $search_data['products.status'] = Product::STATUS_ACTIVE;
        
        // check url to see if any of the following segment pairs are set
        if(isset($uri_search_data['category']))
        {
           $search_data['product_categories.category_name'] = urldecode($uri_search_data['category']);        
        }        
        if(isset($search_data['brand']))
        {
           $search_data['product_brands.brand_name'] = urldecode($uri_search_data['brand']);
        }
        if(isset($search_data['range']))
        {
           $search_data['product_ranges.range_name'] = urldecode($uri_search_data['range']);          
        }
                
        //get number of items per page limit
        $limit = 12;
              
        // new empty product object
        $product = new Product();
        
        // get pagination config array
        $config = array();
        $config["base_url"] = base_url('our-products/browse/' . $this->uri->assoc_to_uri($uri_search_data));
        $config["total_rows"] = $product->count_records();
        $config["per_page"] = $limit;
        $this->pagination->initialize($config);

        // get offset from uri defaulting to 0 if not present
        $offset = end($this->uri->segment_array()) ? end($this->uri->segment_array()) : 0;
                      
        // get all products
        $products = $product->get_where($search_data, $order = NULL, $limit, $offset);
                
        // assign data to view
        $this->view_data['pagination_links'] = $this->pagination->create_links();
        $this->view_data['products'] = $products;
        
        // load default template
        $this->parser->parse('website/products/list.tpl', $this->view_data);
#2

[eluser]Aken[/eluser]
A couple of options:

1) Add a faux "page" sort option to the end, before the pagination URL. Example: /browse/category/accessories/brand/sonia/range/dallas/page/10

2) I can't promise this is the best solution (depends on a few variables), but you could check the total number of URI segments using $this->uri->total_segments(). If it's odd, you know an extra has been stuck on the end, and you can remove it.

Either way, you'll want to sanitize the base_url option of your pagination to make sure that the pagination number is not included. Should be easy.




Theme © iAndrew 2016 - Forum software by © MyBB