Codeigniter: Search and pagination - codeigniter

I want to search and pagination the results but the problem is only the first page works. Because I don't know how to get the search value in the next page.
Here is my code:
Search box
<?php echo form_open('page/search_validation'); ?>
<input type="text" name="search">
<?php echo form_close(); ?>
The first page I can get the search keyword by
$this->input->post('search')
How about the other page? Because after I click the second page the search keyword becomes empty.

Here is an example how to form pagination config for GET method
$search_term= $this->input->get('search');
$config['total_rows'] = $this->db->get('table_name')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 5;
$config['enable_query_strings'] = TRUE;
$config['use_page_numbers'] = TRUE;
$config['query_string_segment'] = 'page';
$config['page_query_string'] = TRUE;
$config['base_url'] = site_url('page/search_validation?search=' . $search_term);
....

Related

Codeigniter: Pagination wont show up

First of all I'm a newbie of Codeigniter.
I'm having problems in showing the pagination links. But when i use search the pagination shows up. Then another problem pops up, when i search there will be links of pagination, when I click the links it doesnt show the paginated search but show the whole non search results.
Controller:
public function info($offset=0)
{
$this->load->library('pagination');
$count = $this->ticketing_mdl->count_all_ticket();
$limit = 4;
$config['base_url'] = "/ticketing/index.php/ticketing/info";
$config['total_rows'] = $count;
$config['per_page'] = $limit;
$config['num_links'] = $limit;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['pagination'] = $this->pagination->create_links();
$data['ticket_list'] = $this->ticketing_mdl->get_all_ticket($limit, $offset);
$this->load->view('ticketing/header');
$this->load->view('ticketing/left_menu');
$this->load->view('ticketing/info',$data);
}
Model:
function get_all_ticket($limit,$page)
{
if($this->input->get('search')){
$match = $this->input->get("search");
$sql = "SELECT * FROM db_ticketing.tr_ticket WHERE requested_by LIKE '%$match%' limit $page,$limit";
return $this->db->query($sql);
}else{
$match = $this->input->get("search");
//$sql = "SELECT * FROM db_contract.bs_contract WHERE contract_tag LIKE '%$match%'";
$sql = "SELECT * FROM db_ticketing.tr_ticket limit $page";
return $this->db->query($sql);
}
}
Change:
return $this->db->query($sql); to
return $this->db->query($sql)->result();
Hope this help!
Fixed my first problem with showing the pagination. now the problem is when i search a keyword the pagination shows up. but when i click the pagination links it does not continues the search result pagination
Controller
if($this->input->get('search')){
$count = $this->ticketing_mdl->count_all_ticket();
$config['total_rows'] = $count;
}else{
$this->db->where('is_valid','1');
$config['total_rows'] = $this->db->count_all_results('db_ticketing.tr_ticket');
}

How to fix codeigniter pagination and route

I have problem whit codeignitet pagination and route!
I set the route my function class like this:
$route['admin/panel/students/new-stuedents-list/:num'] = "admin/newStuedentsList/$1";
then in my controller, I create a function that call newStuedentsList and load pagination library, every things work fine but pagination nav... :(
the page loaded successfully...
and the data is correct...
bat when I click for example page 2 , the page 2 loaded successfully but the pagination nav show page i button ...!
when I call page 4 form url (http:// localhost/d/index.php/admin/panel/students/new-stuedents-list/30) , again the data is correctly ... but the pagination nav show page 1 button and the number of page don't change!
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/d/index.php/admin/panel/students/new-stuedents-list/';
$config['total_rows'] = $this->db->get('new_contest')->num_rows();
$config['pre_page'] = 10;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$adminInfo = $this->admin_model->adminInfo();
$newStudentsList['students'] = $this->admin_model->newStudentsList($config['pre_page'],$this->uri->segment(5));
$data = array_merge($adminInfo,$newStudentsList);
$this->load->view('admin/newStudentsList',$data);
but when the newStuedentsList is:
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/d/index.php/admin/newStuedentsList/';
$config['total_rows'] = $this->db->get('new_contest')->num_rows();
$config['pre_page'] = 10;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$adminInfo = $this->admin_model->adminInfo();
$newStudentsList['students'] = $this->admin_model->newStudentsList($config['pre_page'],$this->uri->segment(3));
$data = array_merge($adminInfo,$newStudentsList);
$this->load->view('admin/newStudentsList',$data);
every things work fine!
How do I fix this problem ...?
You forgot one configuration:
$config['uri_segment'] = 5;
To determines which segment of your URI contains the page number.
And replace pre_page by per_page

Pagination past limit in codeigniter

The code listing below is in a function in a message controller. I want that a page to only have 5 records at a time on a page. Using pagination class in codeignuter i have set the limits meaning that if a page has only three records then in the pagination link in the view only one is shown e.g < 1 and not < 1 2. This also happens with the last page, if the pages are 8 it shows 9 thus <... 8 9. What am i missing
$data['message_available'] = $this->getCheckForMessages();
$config["base_url"] = base_url() . "messages/inbox/";
$config['total_rows'] = $this->getMessageCount();
$config['per_page'] = 5;
$config["num_links"] = 10;
$config["full_tag_open"] = '<div id="pagination">';
$config["full_tag_close"] = '</div>';
$this->pagination->initialize($config);
$data['messages'] = $this->getMessages($config['per_page'], $this->uri->segment(3));
$data['links'] = $this->pagination->create_links();
You have to check deeply this :
$config['total_rows'] = $this->getMessageCount();
maybe is returning some field more than the Results query getMessages();
please post also your getMessages() and getMessageCount(); queries to be more accurated
also please change this:
$config["base_url"] = base_url() . "messages/inbox/";
to this:
$config["base_url"] = site_url('messages/inbox');

Undefined table data

I am using CI 2.1.0 with HMVC. I am going to implement pagination in view part. But I get 'Undefined table data' error. It displays paging number (1,2,3,..) but does not display records.
My code is :
included library for pagination :
$this->load->library('table');
controller
$data['title'] = "Test Events";
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/myproject/index.php/events/test/';
$config['total_rows'] = $this->eventModule->countRow();
$config['per_page'] = '3';
$this->pagination->initialize($config);
$config['records'] = objectsIntoArray($this->eventModule->get_record($config['per_page'], $this->uri->segment(3)));
$data['main_content'] = 'test';
$this->load->view('event/template', $data);
view
<?php echo $this->table->generate('records'); ?>
<?php echo $this->pagination->create_links(); ?>
Thank you for support.
Finally I get the solution. It's silly mistake at
$config['records'] = objectsIntoArray($this->eventModule->get_record($config['per_page'], $this->uri->segment(3)));
It should be $data instead of $config
$data['records'] = objectsIntoArray($this->eventModule->get_record($config['per_page'], $this->uri->segment(3)));

Pagination in codeigniter

Help me my url in pagination ! :(
$data['total'] = $this->news_model->all_list();
$this->load->library('pagination');
$config['base_url'] = site_url().'admin/news/page';
$config['total_rows'] = count($data['total']);
$config['per_page'] = '5';
$this->pagination->initialize($config);
$data['columns'] = array('Tile', 'Date', 'User', 'Active', 'Edit', 'Delete');
$data['list'] = $this->tintuc_model->all_list($config['per_page'],$this->uri->segment(3));
$data['pagination'] = $this->pagination->create_links();
$this->smarty->view( 'admin/news.tpl', $data );
That's my code. List of pages showed ok. But, when all links are wrong.
eg: when i click the 2 page link => http://localhost/mysite/admin/news/page/5 ?
Why it is 5 instead of 2 ?
you can change
$config['per_page'] = '5';
to
$config['per page'] = '2';
in order to get 2 in your uri segment, but it indicates the limit of data to be displayed in your page not the clicked pagination number.
It's not the page number - it's page number * items per page
In your case $config['per_page'] = '5'; So everything seems to be okay.
You need to set:
$config['use_page_numbers'] = TRUE;
http://ellislab.com/codeigniter/user-guide/libraries/pagination.html

Resources