CodeIgniter search input and pagination URL - php

I want to create a URL containing string from search form and page from pagination
For example, my base URL is example.com/search
When showing the search result it will be example.com/search/?s=keyword and when showing the search result from next page it will be example.com/search/?s=term&p=2 for second page and so on
How to make it like that in CodeIgniter?

try this. here used page instead of p in URL
$keyword = trim($this->input->get('s', TRUE));
$this->load->library('pagination');
$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('search/index/?s=' . $keyword);
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
if ($this->input->get('page')) {
$sgm = (int) trim($this->input->get('page'));
$segment = $config['per_page'] * ($sgm - 1);
} else {
$segment = 0;
}
$this->pagination->initialize($config);
// your query
$query = $this->db->select('*')->from('table_name')->limit($config['per_page'], $segment)->get();
now your URL will be same as you mention in your question

Related

Codeigniter pagination not worrking returns 404

Hi I'd like some help please. I 'm having two controllers: movies.php and actors.php where I list all actors and all movies in them.
For example this is the method for listing all my movies
public function index() {
// count all movies
$total = $this->movie_model->count();
// set up pagination
$per_page = 10;
if ($total > $per_page) {
$this->load->library('pagination');
$config['base_url'] = site_url($this->uri->segment(1) . '/');
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(2);
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
// fetch the movies
$this->db->limit($per_page, $offset);
$this->data['movies'] = $this->movie_model->get();
// load the view
$this->view('movies/index');
}
and for listing all actors
public function index() {
// count all actors
$total = $this->actor_model->count();
// set up pagination
$per_page = 10;
if ($total > $per_page) {
$this->load->library('pagination');
$config['base_url'] = site_url($this->uri->segment(1) . '/');
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(2);
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
// fetch the movies
$this->db->limit($per_page, $offset);
$this->data['actors'] = $this->actor_model->get();
// load the view
$this->view('actors/index');
}
I have set my routes like this
$route['default_controller'] = "movies";
$route['404_override'] = '';
$route['movies'] = 'movies/index';
$route['actors'] = 'actors/index';
And the urls are like this
h**p: //localhost/www/task/public/ // for movies (default controller)
h**p: //localhost/www/task/public/actors // for actors controller
The problem is when I try to click a panination link to get the next records in each controller I get a 404 error. I have tried to change my config settings in pagination but no luck.
Any help would be appreciated.
I have link here very help full for pagination and sortable table links this may help.
http://forum.codeigniter.com/thread-1198.html
Change $config['base_url'] = site_url($this->uri->segment(1) . '/'); to,
$config['base_url'] = base_url().'movies/index';
and
$config['base_url'] = base_url().'actors/index';
And $config['uri_segment'] = 3;

codeigniter pagination related questions

$this->load->library('pagination');
$config['base_url'] = base_URL().'admin/issues/list_issues/';
$config['total_rows'] = $this->db->get('dt_issues')->num_rows();
$config['num_links']=10;
$config['per_page'] = 5;
$config['uri_segment'] = 3;
Codeigniter pagination was working properly. But when click on first link it is not working and the remaining links will work. Please help me.
$offset is the variable in which u can get value which page user is reqesting
function list_issues($offset = null)
{
$this->load->library('pagination');
........
// Pagination config
$config['base_url'] = base_URL().'admin/issues/list_issues/';
$config['total_rows'] = $this->provider_model->countprovider();
$config['per_page'] = 3;
// $config['creat_link'] = 1; <-- This is not a valid option
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
// Init config
$this->pagination->initialize($config);
// If the pagination library doesn't recognize the current page add:
$this->pagination->cur_page = $offset;
.....
// further your code
}

CodeIgniter pagination without tables

What i have right now is like 100 divs styled to be 4 in a row, want to add pagination to it, and i succeeded with this code:
public function func() {
$this->load->library('pagination');
$this->load->database('default');
$this->load->model('s_model');
$data['all_rows_s'] = $this->s_model->count_all_s();
$data['$total_s'] = $config['total_s'] = $this->s_model->count_all_ss();
$config['base_url'] = 'http://example.com/display/s/';
$config['total_rows'] = $data['all_rows_s'];
$config['per_page'] = 12;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div class="page_row">';
$config['full_tag_close'] = '</div>';
$data['row'] = $this->db->get('s_data',$config['per_page'], $this->uri->segment(3))->result();
$this->pagination->initialize($config);
$this->template->set_theme(Settings_model::$db_config['active_theme']);
$this->template->set_layout('main');
$this->template->title($this->lang->line('s'));
$this->process_partial('header', '/header');
$this->process_partial('footer', '/footer');
$this->process_template_build('s_view', $data);
}
But, this is working because WITHOUT table because of this line:
$data['row'] = $this->db->get('s_data',$config['per_page'], $this->uri->segment(3))->result();
but i need to make more filters into it, like order by id and where date > NOW(), how is this possible to do ? what ca i replace this get and the code to still work, and again the view is WITHOUT table, is divs within a foreach.
Thanks!
Try this:
Controller Function
function func() {
$this->load->library('pagination');
$this->load->database('default');
$this->load->model('s_model');
$data['all_rows_s'] = $this->s_model->count_all_s();
$data['$total_s'] = $config['total_s'] = $this->s_model->count_all_ss();
/*Pagination*/
$config['base_url'] = 'http://example.com/display/s/';
$config['total_rows'] = $data['all_rows_s'];
$config['per_page'] = 12;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div class="page_row">';
$config['full_tag_close'] = '</div>';
/*Pagination*/
//$data['row'] = $this->db->get('s_data',$config['per_page'], $this->uri->segment(3))->result();
$data['row'] = $this->s_model->get_records($config['per_page'], $this->uri->segment(3));
$this->pagination->initialize($config);
$this->template->set_theme(Settings_model::$db_config['active_theme']);
$this->template->set_layout('main');
$this->template->title($this->lang->line('s'));
$this->process_partial('header', '/header');
$this->process_partial('footer', '/footer');
$this->process_template_build('s_view', $data);
}
Model Function
function get_records($limit, $offset = 0){
return $this->db->where('date > NOW()')->order_by('id', 'asc')->get('s_data', $limit, $offset)->result();
}

how to route pagination next page in uri->segment(1) eg (example.com/1)

I have pagination in frontend index page example.com when I click page two of pagination I want to load page like eg example.com/1.
<?php
function index()
{
$e = $this->employer_model->count_jobs_with_employer();
//print_r($e) ;
$config['base_url'] = $this->config->site_url(); //set the base url for pagination
$config['total_rows'] = $e; //total rows
$config['per_page'] = '10'; //the number of per page for pagination
$config['uri_segment'] = 1; //see from base_url. 3 for this case
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config); //initialize pagination
$page = ($this->uri->segment(1)) ? $this->uri->segment(1) : 0;
$joblist['listemp'] = $this->employer_model->employerbyjob($config['per_page'], $page);
foreach($joblist['listemp'] as $emp)
{
$joblist['jobs'][$emp->u_id] = $this->employer_model->get_ByTbl_Col('employer_post_job', 'e_id', $emp->u_id,'dead_line', date('Y-m-d'));
}
$c['job_list'] = $this->load->view('front/blocks/employer/job_list', $joblist, TRUE);
}
?>
Something like this should work:
application/config/routes.php:
//default routes:
$route['default_controller'] = "yourcontroller";
$route['404_override'] = '';
// your custom route:
$route['(:num)'] = "yourcontroller/index/$1";
application/controllers/Yourcontroller.php
function index($page = null)
{
}

codeigniter pagination get url

I have a site developed in codeigniter and in a page I want to use pagination.
The problem is. my url now is smoething like that:
http://site/index.php/tee/view_tee/?id=2
This is my code into the controller:
$data['tee'] = $this->Tee_model->getTeeByUserId($this->input->get('id', TRUE));
$data['tee_like'] = $this->Tee_model->getLikeTeeByUserId($this->input->get('id', TRUE));
$data['user'] = $this->User_model->getUserById($this->session->userdata('id'));
$this->load->library('pagination');
$config['base_url'] = site_url().'/tee/view_tee/?id='.$this->input->get('id', TRUE);
$config['total_rows'] = count($data['tee']);
$config['per_page'] = 6;
$config['uri_segment'] = 3;
if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
$config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
$data['page_links']=$this->pagination->create_links();
$this->pagination->initialize($config);
$this->load->view('view_tee_view',$data);
With this code when I click into my pagination link I have this url:
http://site/index.php/tee/view_tee/?id=2/6?id=2
If I click again I have this:
http://site/index.php/tee/view_tee/?id=2/6?id=2/6?id=2
This is my html:
echo $this->pagination->create_links();
foreach($tee as $t){
// I have three div per lines
}
add this...
$config['page_query_string'] = TRUE;
remove this...
if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
$config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
[EDIT] WORKING EXAMPLE
$this->load->library('pagination');
$config['page_query_string'] = TRUE;
$config['base_url'] = site_url().'/tee/view_tee/?id='.$this->input->get('id', TRUE);
$config['total_rows'] = 200;
$config['per_page'] = 6;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
echo $this->pagination->create_links();

Categories