Filter, sort and paginate in Codeigniter

I recently started using CodeIgniter when I was looking for a very lightweight framework, and it seemed to be the best choice.

I am new to all MVC work, enjoying it, but stuck with something that seems very simple.

I encode CMS and need a way to filter, sort, and paginate results. I'm used to doing this with querystrings, so I would have something like:

articles.php?order=title&sort=desc&filter=articletitle&page=5

I have no idea how to do this in CI, so I just turned on EnableQueryStrings in the config, and it works fine, but I feel that this is probably not the most elegant solution.

I suppose that

index.php/articles/index/order/title/sort/desc/filter/articletitle/page/5

but for me it seems very inflexible, that if, for example, I don’t need to sort, how would I make sure that I am looking at the correct uri segment?

any ideas?

+3
6

_remap? , ( ) , .

- :

class Articles extends Controller
{
    // Note: No need for a "order" or even an "index" function!
    public function _remap()
    {
        $assoc = $this->uri->uri_to_assoc( 4 );
        /* $assoc is now 
           array( "order"=>"title",
                  "sort"=>"desc",
                  "filter"=>"articletitle", 
                  "page"=>5); */
    }
}
+3

, , .

/ ( , , ).

URL- :

http://www.myapp.dev/controller/index/10?order_by=id+asc&status=open

$_GET , , , pg- CI.

CodeIgniter URI. CI : http://pastie.org/1393513

:

$config['url_format'] = site_url('controller/index/{offset}?'.http_build_query($params));
$config['total_rows'] = $this->model->count_rows();
$config['per_page'] = 5;
$this->pagination->initialize($config);

, uri_segment, Pagination:: initialize , {offset} url_format.

, $this->pagination->create_links(), .

+2

, CodeIgniter . . URI, .

URL-, , :

index.php/articles/index/order/title/sort/desc/filter/articletitle/page/5

CodeIgniter URI . , .

0

, . , , .

0

- :

www.yourdomain.com/articles/order-title/sort-desc/filter-articletitle/page-5

index.php .htaccess. "-" URI.

$order = $this->uri->segment(2, 0);
$sort = $this->uri->segment(3, 0);
$filter = $this->uri->segment(4,0);
$page = $this->uri->segment(5, 0;

if(!empty($order)){
    $order = explode('-', $order);
} else {
    $order = 'defaultorder';
}

/** And so on for the rest of the URI segments **/

:

www.yourdomain.com/articles/title/desc/articletitle/5

$order = $this->uri->segment(2, 0);
$sort = $this->uri->segment(3, 0);
$filter = $this->uri->segment(4,0);
$page = $this->uri->segment(5, 0;

, Code Igniter, .

0

/ , , , , .

, .

http://mysite.com/admin/contacts?status=open&per_page=20

page_query_string pagination. :

$query_string = some_function_to_get_your_filter_query_string_params();

$config['base_url'] = site_url("admin/contacts?". $query_string);
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
0

Source: https://habr.com/ru/post/1736561/


All Articles