• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you make pagination with search, sort by, order by and limit work?

#1
[eluser]Warz[/eluser]
Hi,

I'm basically just looking for some tips on how to deal with pagination, the best way to do it...

Right now I got this URL:
http://localhost/memberbase/index.php/admin/users/index

This page is supposed to display a list of all users. I need the following:
- Search for username ($_GET search)
- Sort by ID, Username, Email, Country, etc. Both ASC and DESC
- Option to change amount of users displayed per page

This works fine:
http://localhost/memberbase/index.php/admin/users/index/2

I also added support for sort by and order by, which looks like this:
http://localhost/memberbase/index.php/admin/users/index/2/sortby/username/orderby/asc
http://localhost/memberbase/index.php/admin/users/index/2/sortby/username/orderby/desc
etc..
Now, is it so, that I really need to do this:
http://localhost/memberbase/index.php/admin/users/index/2/sortby/username/orderby/asc/perpage/10/search/john

to search for user john?

Is it possible to just go
http://localhost/memberbase/index.php/admin/users/index/search/john
and then later do the sort by, order by and limit?

I know this is possible with $_GET however when turning that on I get

Quote:An Error Was Encountered

The URI you submitted has disallowed characters.

All this just seems rather confusing and not very user friendly? Then maybe there is something I didn't know? What is best practise? How do YOU deal with pagination?

Thanks! Smile

#2
[eluser]Kinsbane[/eluser]
I would honestly set up a custom route in your config/routes.php file that sends certain kinds of search queries to certain kinds of controllers so you can control exactly how the data goes through your system.

I don't have a searching method, but I did build a documents library for my company and this is how I handled all the different things (including pages, ASC/DESC, and sort by/order by. This is an example of my routes file:
Code:
$route['documents/case-studies'] = 'documents/casestudies/index/all/0/last_modified/DESC';
$route['documents/case-studies/(:any)/(:num)/(:any)/(:any)'] = "documents/casestudies/index/$1/$2/$3/$4";
$route['documents/case-studies/fiber-driver'] = "documents/casestudies/index/fiber-driver/0/last_modified/DESC";
$route['documents/case-studies/lambda-driver'] = "documents/casestudies/index/lambda-driver/0/last_modified/DESC";
$route['documents/case-studies/lx-series'] = "documents/casestudies/index/lx-series/0/last_modified/DESC";
$route['documents/case-studies/media-cross-connect'] = "documents/casestudies/index/media-cross-connect/0/last_modified/DESC";
$route['documents/case-studies/pluggables'] = "documents/casestudies/index/pluggables/0/last_modified/DESC";
$route['documents/case-studies/optiswitch'] = "documents/casestudies/index/optiswitch/0/last_modified/DESC";
$route['documents/case-studies/optiswitch-mr'] = "documents/casestudies/index/optiswitch-mr/0/last_modified/DESC";
$route['documents/case-studies/terescope'] = "documents/casestudies/index/terescope/0/last_modified/DESC";
$route['documents/case-studies/service'] = "documents/casestudies/index/service/0/last_modified/DESC";
$route['documents/case-studies/management'] = "documents/casestudies/index/management/0/last_modified/DESC";

As far as pagination goes, I actually had to modify the stock Pagination library that came with CI in order to include how the user is sorting, and by what they are sorting. Sample function:
Code:
function browse($family, $offset, $sortby, $sorthow)
{
$data['sorthow'] = ( $sorthow == "ASC" ) ? "DESC" : "ASC";
$data['offset'] = $offset;
$data['sortby'] = $sortby;
$docs = $this->docs->getDocuments('application-notes', $family, $this->num, $offset, $sortby, $sorthow);

//the following variables pertain to columns in the table, the headers, that the user can click on to change the sort:
$data['sort1url'] = $this->config->item('base_url').'documents/application-notes/'.$family.'/'.$offset.'/title/'.$data['sorthow'];
        $data['sort2url'] = $this->config->item('base_url').'documents/application-notes/'.$family.'/'.$offset.'/content_type/'.$data['sorthow'];
        $data['sort3url'] = $this->config->item('base_url').'documents/application-notes/'.$family.'/'.$offset.'/last_modified/'.$data['sorthow'];
//sorting by content type, title, and when the entry was last modified


//and finally set up the pagination
$config['uri_segment'] = 4; //I've made my program so that I always know the pagination segment will be 5
$config['base_url'] = $this->config->item('base_url').'documents/application-notes/'.$family;
$config['total_rows'] = count($this->docs->getTotalDocuments('application-notes', $family));
$config['per_page'] = $this->config->item('per_page_count');
$this->pagination->initialize($config);
$data['pages'] = $this->pagination->create_links('/'.$sortby.'/'.$sorthow); //I'm passing in my $sortby and $sorthow to the Pagination class so those values can be added to the paging numbers by the Pagination library

Now, I understand this has nothing to do with searching - but I think you're on the right path and I hope what I've posted can give you some ideas or inspiration on where to go.

I see you got the error regarding the disallowed characters - another option is to temporarily store the search term in a hash string and use that

#3
[eluser]predat0r[/eluser]
Check this for pagination and sort:

http://net.tutsplus.com/tutorials/php/co...ular-data/

and this for search:

http://net.tutsplus.com/tutorials/php/co...strings-2/


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.