Reputation: 3
I am working on a site in CodeIgniter.
I want my site users to select the city from a drop-down and then the content to be displayed on the basis of that city. For this I have two issues.
1) How can I add a new parameter to the URL segment. I checked this . Is it ok to create city as controller? but then how should I create the current controllers? If so,
2) Problem is I have already worked on all controllers.
Guide me on how should I proceed.
Upvotes: 0
Views: 895
Reputation: 1155
try this. -first tell me how u send request after selecting a city from dropdown. -anyways i will tell you ,first add jquery 'change' event to dropdown list and on change you have to get the current value of the dropdown list as e.g
$('#idOfDropDownlist').on('change',function(){
var value = $(this).val();
//now send a ajax request to controller to get information about city.
$.ajax({
type:'get',
url:"<?php echo base_url(); ?>ControllerName/methodName/"+value,
success:function(response){
console.log(response);
}
});
});
//---- your controller's method for getting this request will be like.
class ControllerName .....{
public function methodName($city = ''){
//--- here you got the city name,so do whatever u want with......
}
}
Upvotes: 0
Reputation: 169
You could pass the uri segments as parameters to your controller.
http://YOUR_URL.com/index.php/city/get/zurich
<?php
class City extends CI_Controller {
public function get($city)
{
echo $city;
}
}
http://www.codeigniter.com/user_guide/general/controllers.html#passing-uri-segments-to-your-methods
Edit
Just to give you an idea:
First remove the index.php from the URL:
create the .htaccess
file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# When CI is in a subfolder use this line instead
#RewriteRule ^(.*)$ /ci/index.php/$1 [L,QSA]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Open the file /application/config/config.php
and search for the line
$config['index_page'] = 'index.php';
and change it to
$config['index_page'] = '';
Open the file /application/config/routes.php
and add this line to the other rules
$route['(:any)/(:any)/(:any)'] = "$2/$3/$1";
And the controller looks like this.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class City extends CI_Controller {
public function index($city)
{
echo $city;
}
}
So I'm just changing the order of the segments. 2 = Class, 3 = Method, 1 = parameters.
Upvotes: 1