• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pagination with search

#1
I've had some issues searching with pagination. I've resolved it with the following code, but I feel I'm basically doing something fundamentally wrong. Could someone check and give me feedback about what this could be? Thanks.

PHP Code:
public function read()
{
 
 $this->load->library('pagination');
 
 $offset 0;
 
 $config['base_url'] = base_url("language/read");
 
 $config['per_page'] = 3// sql query LIMIT number
 
 $offset = ( $this->uri->segment(3) ? $this->uri->segment(3) : );

 
 // set search
 
 if$this->input->post('search') || $this->input->get('search') ) {
 
   $search = ( $this->input->post('search') ? $this->input->post('search') : $this->input->get('search') );
 
 }
 
 else {
 
   // user has hit return in an empty input field, or has clicked a query link with no property value
 
   $search NULL;
 
 }

 
 $config['suffix'] = "?search=$search"// attaches a query onto $this->uri->segment(3), aka $offset

 
 // !issue fix! user searches while offset is out of range of search result
 
 if$offset != && $this->input->post('search') ){
 
   redirect("language/read/0?search=$search");
 
 }
 
 // !issue fix! user searches while no GET query is in URL
 
 if$offset != && $this->input->get('search') === NULL ){
 
   redirect("language/read/0?search=$search");
 
 }

 
 $this->data['languages'] = $this->language_model->get_languages_pagnation($config['per_page'], $offset$search);
 
 $config['total_rows'] = $this->db->query("SELECT id FROM languages WHERE title LIKE '%$search%'")->num_rows();
 
 $config['first_url'] = "0?search=$search"// explicitly set this to the first link

 
 $this->pagination->initialize($config);
 
 $this->data['pagination'] = $this->pagination->create_links();
 
 $this->render('language');

Reply

#2
Not entirely sure what you're doing but I'd recommend using js datatables saves the need to create pagination, search, sort columns.

Where you would have to define separate models and controllers.

It has saved me a ton of work.
IgnitedCMS Pro
IgnitedCMS
Practical guide to IgnitedCMS - Book coming soon
Reply

#3
Thanks for the new perspective. I had never thought about doing pagination on the front end.
Reply

#4
JS datables is great, but depending on what kind of clients are running your front end, it can be an issue. It won't matter with a small set of data, but I've had to switch away from datatables on a few occasions because it can absolutely rape some browsers.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply

#5
(02-24-2017, 01:32 AM)albertleao Wrote: JS datables is great, but depending on what kind of clients are running your front end, it can be an issue. It won't matter with a small set of data, but I've had to switch away from datatables on a few occasions because it can absolutely rape some browsers.

That's a good point, I always wondered what the performance would be on a significantly large dataset. What browsers have you had issues with, I'm assuming it is earlier version of the dreaded Internet explorer?
IgnitedCMS Pro
IgnitedCMS
Practical guide to IgnitedCMS - Book coming soon
Reply

#6
If you do stick to CI pagination, you can shorten your code by using $this->input->post_get('search');
The documentations says this about it:
This method works pretty much the same way as post() and get(), only combined. It will search through both POST and GET streams for data, looking in POST first, and then in GET.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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