data not filtering in ajax codeigniter

ぃ、小莉子 提交于 2020-01-06 08:44:10

问题


I want to filter data by dropdown but it giving me all data....

this is my model with ajax function

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();   
}

And this is my controller

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>'; 
    }
}

This is it looks after running code...

it giving me all data instead of selected data

https://i.imgur.com/SdwzKJ9.png


回答1:


Here you have to pass the variable that you are sending to the Model. You are not passing the variable to model that was not getting in your model's function.

Check this below that may help.

Controller

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>'; 
    }
}

Model

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:


SO for filtering data with dropdown you need to pass the values of dropdown to controller i.e. city name and include it to your query and call ajax() at onChange() in view of codigniter




回答3:


Look like you do not pass your filtering arguments to ajax model. When you run the script it only execute $query = "SELECT * from info_user Where user_status ='1'"; part. In this case the out-put will be all data where user_status = 1.

if(!empty($size)) and if(!empty($sprice) && !empty($eprice)) conditions are always false.

So try to inject filtering pentameters to ajax model.




回答4:


Your construction of model and controller has very big security problems about sql queries.

Allways try to use Query Builder for generate SQL queries for safer queries.

I corrected your model and controller construction how it must be below:

Controller

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
    }

}

Model

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.

}


来源:https://stackoverflow.com/questions/49913919/data-not-filtering-in-ajax-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!