Codeigniter pagination routing issue - codeigniter

I have configured a route in routes.php as,
$route['job-history/(:num)'] = "customer/customer/jobhistory/$1";
and pagination configuration in controller as follows,
$this->load->library('pagination');
$config['per_page'] = 25;
$config['total_rows'] = 100;
$config['base_url'] = $this->config->item('base_url').'job-history';
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(3) : 0;
Its showing 404 error page while loading,
www.example.com/job-history
It will work if manually add a zero like www.example.com/job-history/0.
How can I load www.example.com/job-history as first page. Whats wrong in my confuration. Any help please

You'll need a route for the single job-history segment as well.
$route['job-history/(:num)'] = 'customer/customer/jobhistory/$1';
$route['job-history'] = 'customer/customer/jobhistory';

Since, in route.php, you have only mentioned for job-history pages that has a number segment after it and there is no such rule for your page job-history only, it gets redirected to 404.
Add
$route['job-history'] = 'customer/customer/jobhistory';
before
$route['job-history/(:num)'] = 'customer/customer/jobhistory/$1';

Related

codeigniter Different/separate 404 page for frontend and backend

How can set auto route 404 page of frontend and backend different ? I have searched a long time but i have not found perfect answer.
Btw, can we make the same for error 500 ?
You can easily do it by using conditional statement.
go to application/config/routes.php and remove:
$route['404_override'] = '';
After that add the following code.
$req_uri = $_SERVER['REQUEST_URI']; // $req_uri = /myproject/backend
$req_uri = explode('/', $req_uri);
$req_uri = $req_uri[2]; // $req_uri[2] = backend
if($req_uri == 'backend'){
$route['404_override'] = 'Backend_error'; // Not found controller for backend
}else {
$route['404_override'] = 'Frontend_error'; // Not found controller for frontend
}
You can use echo statement to analyze further. and then do more stuff accordingly.

Codeigniter URI routing with for multiple methods

I am working on codeigniter. Controller name is "project" and 4 functions are there.."get_city_data", "get_store_data", "get_currency", "get_description".
Parameters I am passing in above 2 functions are - get_city_data($state, $city), get_place_data($state, $city, $store).
So on browser to render these functions I am using the urls respectively as-
http://localhost/project/get_city_data/state1/city1
I want to change the urls like
http://localhost/state1/city1
In routes.php if I define a route like this
$route['(:any)/(:any)'] = 'project/get_city_data/$1/$2' then it reflects for all other functions as well but I want to change urls for these 2 functions only.
Also I do not want to use _remap function as it doesn't go well with my needs.
Can anybody help me in this?
ThankYou
I suggest you to check in the database at first, then determine the routes required. Here is my answer for your requirement.
In your routes.php, try this code:
require_once( BASEPATH . 'database/DB' . EXT );
$url_source = $_SERVER['REDIRECT_URL'];
$arr_url = explode("/", $url_source);
$state_name = $arr_url[3];
$city_name = $arr_url[4];
$store_name = $arr_url[4];
$db = & DB();
//Let say grab 3 columns to be check
$db->select('state_name, city_name, store_name');
if(!empty($state_name)):
$db->where('state_name', $state_name);
endif;
if(!empty($city_name)):
$db->where('city_name', $city_name);
endif;
if(!empty($store_name)):
$db->where('store_name', $store_name);
endif;
$query = $db->get('table', 1);
$obj = $query->row();
if ($obj):
$route["$obj->state_name/$obj->city_name/$obj->store_name"] = "project/get_city_data/$state_name/$city_name/$store_name";
endif;
You can try by this url to test:
http://localhost/state_name/city_name/store_name
I got it solved using regex.
In routes.php i wrote
$url_source = $_SERVER['REQUEST_URI'];
if(!(preg_match('%^/project/get_description\?state=[a-zA-Z_-]*%', $url_source) || preg_match('%^/project/get_currency\?state=[a-zA-Z_-]*%', $url_source))){
if(!preg_match('%^\/[a-zA-Z_-]+\/[a-zA-Z_-]+\/[a-zA-Z_-]+$%', $url_source)){
$route['(:any)/(:any)'] = 'project/get_city_data/$1/$2';
}else{
$route['(:any)/(:any)/(:any)'] = 'project/get_store_data/$1/$2/$3';
}
}
This way I was able to route for particular functions.

search result from sql error with codeigniter's pagination index

I have a table with the pagination. First, i select all data from my DB, and the pagination works perfectly.
Then, i started to filter the data (search certain data, example : using WHERE in SQL). The pagination's 1st index is working perfectly (showing only certain data) but when i click the next index (2nd, 3rd) the data is going back to default (the filter (WHERE, LIKE) is not working).
This is my model that used for the pagination (I think i made a mistake here) :
public function get_umat() {
$this->db->select('*')->from('msumat')->limit(10, $this->uri->segment(3));
$this->db->join('mskelas', 'msumat.kelas_id = mskelas.kelas_id');
$search = $this->input->post('ddl_search');
$kelas = $this->input->post('ddl_kelas');
$kelas1 = $this->input->post('ddl_kelas1');
$kelas2 = $this->input->post('ddl_kelas2');
$nama = $this->input->post('txt_nama');
$alamat = $this->input->post('txt_alamat');
$bulan = $this->input->post('ddl_bulan');
$bulan1 = $this->input->post('ddl_bulan1');
$bulan2 = $this->input->post('ddl_bulan2');
if($this->input->post('btn_search'))
{
if($search === 'nama')
$this->db->like('nama', $nama);
else if($search === 'kelas')
$this->db->where('mskelas.kelas_id', $kelas);
else if($search === 'range_kelas')
$this->db->where("mskelas.kelas_id BETWEEN $kelas1 AND $kelas2");
else if($search === 'alamat')
$this->db->like('alamat', $alamat);
else if($search === 'bulan_lahir')
$this->db->where("MONTH(tanggal_lahir) = $bulan");
else if($search === 'range_tanggal_lahir')
$this->db->where("MONTH(tanggal_lahir) BETWEEN $bulan1 AND $bulan2");
}
return $this->db->get()->result();
}
public function count_umat(){
return $this->db->count_all('msumat');
}
Then this is my pagination in controller (I think i need to modify the $config['total_rows']):
$config['base_url'] = site_url('/backend_umat/index');
$config['total_rows'] = $this->umat_m->count_umat();
$config['per_page'] = 10;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
I think the problems are :
I need to COUNT the search results (for each search) for $config['total_rows']
Somehow, i need to modify the model (get_umat()) to get things working
Any help is appreciated. Thanks :D
well in CI if you want to apply search with multiple filters it works for the first page and ci always look for url segments, for 2nd or 3rd page you need to send your all search parameters which you send for the first page through form, means you url should contain url segments then you can filter your search record because you are not submitting form for 2nd and 3rd page. i will suggest you to have look on this tutorial it has nice way to use search records with multiple filters and work perfect with pagination
CI Search on Tutorial Nettuts

CodeIgniter: Understanding routes

I have the this route defined in routes.php
$route['user'] = "user_controller";. The user controller has a method logout() but when I try this URI user/logout I get a 404. In the same way when I use this URI user/index I get a 404.
routes.php
// custom routes
$route['start'] = "start_controller";
$route['register'] = "register_controller";
$route['user'] = "user_controller";
// other routes
$route['default_controller'] = "start_controller";
$route['404_override'] = '';
According to CI
Note: Routes will run in the order they are defined. Higher routes
will always take precedence over lower ones.
$route['default_controller'] and $route['404_override'] must always be on top above others
$route['user/logout'] = "user_controller/logout";
$route['user/index'] = "user_controller";
Example i will type a user/logout then it will proceed to user_controller/logout you have to define the URL you would like to redirect
Yep, you have to specify a route for each particular method.
Here's an example from my routes.php :
/* User Authentication Controller */
$route['login'] = "auth/login";
$route['logout'] = "auth/logout";
$route['register'] = "auth/register";
$route['forgot'] = "auth/forgot";

Codeigniter Pagination bug

I think that there is a bug in the codeigniter pagination library. For some reason when I generate the pagination links, it generates the links as:
1 2 3 4
Where page 3 is linking to 4.
Here is the config variables code in case anyone is curious:
$config['base_url'] = base_url() . "index.php/test/$query_string";
$config['total_rows'] = $search_results->num_rows();
$config['per_page'] = $items_per_page;
And here is a sample of my query string:
?q=sample_query_string&per_page=1
Is there a way to fix this?
I wouldn't bother with the query string, use a variable passed straight from the url/controller. Also, I think your base url is wrong. It should be (assuming your are on the default index function page)
$config['base_url'] = site_url("test/index");
You don't need to put the vars at the end of the base url. If you have query strings enabled (i don't think you do though), this would be the same, CI should handle all the renaming, just extracting the variables will be different.
So controller should be
class Test extends Controller {
function index($offset = 0)
{
$this->load->library('pagination');
$config['base_url'] = site_url("test/index");
$config['total_rows'] = $search_results->num_rows();
$config['per_page'] = 20;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
// DO OTHER STUFF
}
}
You can set the limit in your config. Do you need to do it from the URL?

Resources