我想通过下拉列表过滤数据,但它会为我提供所有数据....
这是我的ajax函数模型
public function ajax()
{
$query = "SELECT * from info_user Where user_status ='1'";
if(!empty($size)){
$query .= " and city in('".$size."')";
}
if(!empty($sprice) && !empty($eprice)){
$query .= " and charge_per_hour >='".$sprice."' and
charge_per_hour <='".$eprice."'";
}
$result = $this->db->query($query);
return $result->result();
}
这是我的控制器
public function ajax()
{
$size = $this->input->post('size');
$sprice = $this->input->post('sprice');
$eprice = $this->input->post('eprice');
$this->load->model('ajax_model');
$result = $this->ajax_model->ajax();
foreach( $result as $row )
{
echo $row->name.'<br>';
echo $row->charge_per_hour.'<br>';
echo $row->city.'<br>';
}
}
这是运行代码...
它为我提供了所有数据而不是选定的数据
答案 0 :(得分:2)
因此,要使用下拉列表过滤数据,您需要将下拉列表中的值传递给控制器,即城市名称,并将其包含在您的查询中,并在 ajax() > onChange()鉴于codigniter
答案 1 :(得分:1)
您必须将要发送的变量传递给模型。 您没有将变量传递给未进入模型函数的模型。
请查看以下内容,这可能会有所帮助。
<强>控制器强>
public function ajax()
{
$size = $this->input->post('size');
$sprice = $this->input->post('sprice');
$eprice = $this->input->post('eprice');
$this->load->model('ajax_model');
$result = $this->ajax_model->ajax($size,$sprice,$eprice);
foreach( $result as $row )
{
echo $row->name.'<br>';
echo $row->charge_per_hour.'<br>';
echo $row->city.'<br>';
}
}
<强>模型强>
public function ajax($size = '',$sprice = '',$eprice = '')
{
$query = "SELECT * from info_user Where user_status ='1'";
if($size != '')){
$query .= " and city in('".$size."')";
}
if(($sprice != '') && ($eprice != '')){
$query .= " and charge_per_hour >='".$sprice."' and
charge_per_hour <='".$eprice."'";
}
$result = $this->db->query($query);
return $result->result();
}
答案 2 :(得分:1)
看起来你没有将过滤参数传递给ajax模型。运行脚本时,它只执行$query = "SELECT * from info_user Where user_status ='1'";
部分。在这种情况下,输出将是 user_status = 1的所有数据。
if(!empty($size)) and if(!empty($sprice) && !empty($eprice)) conditions are always false.
因此,尝试将过滤五边形注入ajax模型。
答案 3 :(得分:1)
你的模型和控制器的构建对于sql查询有很大的安全性问题。
总是尝试使用Query Builder为更安全的查询生成SQL查询。
我纠正了你的模型和控制器结构如何必须在下面:
<强>控制器强>
public function ajax(){
$size = $this->input->post('size');
$sprice = $this->input->post('sprice');
$eprice = $this->input->post('eprice');
$this->load->model('ajax_model');
$result = $this->ajax_model->ajax( $size , $sprice , $eprice );
foreach( $result as $row ) {
echo '<p>';
echo $row['name'].'<br/>';
echo $row['charge_per_hour'].'<br/>';
echo $row['city'].'<br/>';
echo '</p><hl>';//split between rows
}
}
<强>模型强>
public function ajax( $size = null , $sprice = null , $eprice = null ){
$this->load->database();//if not loaded automatically.
$this->db->select('*')->from('info_user')->where('user_status',1);
if($size){
$size=explode(',',$size);//i think your post value for $size delimited with comma (,).
$this->db->where_in('city',$size);
}
if($sprice){//$this->input->post('sprice') returns false when it is not provided. You may control it is false or not.
$this->db->where('charge_per_hour>=',$sprice);
}
if($eprice){
$this->db->where('charge_per_hour<=',$eprice);
}
$result = $this->db->get();
return $result->result_array();//using array needs a little lower system resuorces then objects.
}