PJunior
PJunior

Reputation: 2767

CodeIgniter - How to add strings to pagination link?

I would like to add some strings/values to the end of the generated pagination link. For example, I get this

http://localhost/products/lists/5

I would like to have

http://localhost/products/lists/5/value/anothervalue

So, I need to send those values somehow... :)

Thank u all.

Upvotes: 1

Views: 736

Answers (2)

Samutz
Samutz

Reputation: 2300

The pagination class has an undocumented configuration option called suffix that you can use. Here's how I use it in one of my apps:

// get the current url segments and remove the ones before the suffix
// http://localhost/products/lists/5/value/anothervalue
$args = $this->uri->segment_array();
unset($args[1], $args[2], $args[3]);
$args = implode('/', $args);

// $args is now 'value/anothervalue'

$base_url = 'http://localhost/products/lists';

$this->pagination->initialize(array(
    'base_url' => $base_url,
    'suffix' => '/'.$args,
    'first_url' => $base_url.'/1/'.$args,
    'uri_segment' => 3
));

Upvotes: 1

Andrei Zhamoida
Andrei Zhamoida

Reputation: 1464

The application/config/routes.php

 $route['products/lists/(:num)/value/(:any)'] = "products/lists/$1/$2";

The controller code application/controllers/products.php

class Products extends CI_Controller {
    public function index() {
        $this->load->view('welcome_message');
    }

    public function lists($page = 1, $value = null) {
        $this->load->view('product_lists', array('page' => $page, 'value' => $value));
    }
}

In this way if your url is like http://localhost/products/lists/5/value/anothervalue in function lists will be $page = 5 and $value = 'anothervalue' and they will be available in template product_lists ($page, $value)

Upvotes: 0

Related Questions