http://rimi-classified.com/ad-list/west-bengal/kolkata/electronics-and-technology
上面的链接左侧有一个过滤器。我正在尝试使用ajax从州获得城市。但是当触发ajax时,整个查询都在改变。
SELECT * FROM (`ri_ad_post`)
WHERE `state_slug` = 'west-bengal'
AND `city_slug` = 'kolkata'
AND `cat_slug` = 'pages'
AND `expiry_date` > '2014-03-21'
ORDER BY `id` DESC
它在查询中取控制器名称(控制器名称是页面)。
实际查询是:
SELECT *
FROM (`ri_ad_post`)
WHERE `state_slug` = 'west-bengal'
AND `city_slug` = 'kolkata'
AND `cat_slug` = 'electronics-and-technology'
AND `expiry_date` > '2014-03-21'
ORDER BY `id` DESC
//控制器
public function ad_list($state,$city,$category,$sub_cat=FALSE)
{
if($state===NULL || $city===NULL || $category===NULL)
{
redirect(base_url());
}
if ($sub_cat === FALSE)
{
$data['ad_list'] = $this->home_model->get_adlist($state,$city,$category);
}
$data['ad_list'] = $this->home_model->get_adlist($state,$city,$category,$sub_cat);
$this->load->view('templates/header1', $data);
$this->load->view('templates/search', $data);
$this->load->view('ad-list', $data);
$this->load->view('templates/footer', $data);
}
public function get_cities()
{
$state_id = $this->input->post('state');
echo $this->city_model->get_cities($state_id);
}
// home model
public function get_adlist($state,$city,$category,$sub_cat=FALSE)
{
if ($sub_cat === FALSE)
{
$this->db->where('state_slug', $state);
$this->db->where('city_slug', $city);
$this->db->where('cat_slug', $category);
$this->db->where('expiry_date >', date("Y-m-d"));
$this->db->order_by('id', 'DESC');
$query = $this->db->get('ad_post');
}
$this->db->where('state_slug', $state);
$this->db->where('city_slug', $city);
$this->db->where('cat_slug', $category);
$this->db->where('sub_cat_slug', $sub_cat);
$this->db->where('expiry_date >', date("Y-m-d"));
$this->db->order_by('id', 'DESC');
$query = $this->db->get('ad_post');
return $query->result_array();
//echo $this->db->last_query();
}
//状态模型
class State_model extends CI_Model {
public function __construct() {
$this -> load -> database();
}
function get_states() {
$query = $this->db->order_by('state', 'ASC')->get('state');
return $query->result_array();
}
}
//城市模型
class City_model extends CI_Model {
public function __construct() {
$this -> load -> database();
//$this->output->enable_profiler(TRUE);
}
function get_cities($state){
if($state != NULL){
$this->db->where('state_id', $state);
$query = $this->db->get('city');
$cities = array();
$html = '';
if($query->result())
{
$html .= '<select id="city_id" name="city_id">';
$html .= '<option value="">-- Select City --</option>';
foreach ($query->result() as $city)
{
$html .= '<option value="'.$city->id .'">'.$city->city.'</option>';
}
$html .= '</select>';
return $html;
}
else
{
return FALSE;
}
}
else
{
$html = '<option value="">--Select City--</option>';
return $html;
}
}
}
// AJAX
<script type="text/javascript">
$(document).ready(function () {
$('#state_id').change(function () {
var selState = $(this).val();
alert(selState);
console.log(selState);
$.ajax({
url: "pages/get_cities",
async: false,
type: "POST",
data : "state="+selState,
dataType: "html",
success: function(data) {
$('#city').html(data);
$("#location_id").html("<option value=''>--Select location--</option>");
}
})
});
});
</script>
请帮我解决这个问题。请检查我提供的网址,并尝试从过滤器部分选择状态,问题将更加明确。
答案 0 :(得分:0)
在控制器中尝试此操作
public function get_cities()
{
$state_id = $this->input->post('state');
$cities = $this->city_model->get_cities($state_id);
echo '<option value="">-- Select City --</option>';
foreach($cities as $city){
echo '<option value="'.$city->id .'">'.$city->city.'</option>';
}
}
在模型中
function get_cities($state){
$this->db->where('state_id', $state);
$query = $this->db->get('city');
return $query->result();
}
在ajax中
success: function(data) { $('#city_id').html(data)}