简体   繁体   English

Codeigniter不会根据where条件从数据库返回数据

[英]codeigniter not returning data from database based on where condition

i am trying to get get some data through an ajax call in codeigniter based on the user input but i keep getting all result from the database instead of the matching ones. 我试图通过基于用户输入的codeigniter中的ajax调用获取一些数据,但是我一直从数据库而不是匹配项中获取所有结果。

here is my ajax: 这是我的ajax:

$(document).ready(function () {
    $(".filter").keyup(function () {
        var keyword= $("#loc").val();
        $.ajax({
            type: "POST",
            url: CFG.url + '/pages/filter_hotel_by_loc/',
            data: keyword,
            dataType: "json",
            success: function (data) {
                console.log(data);
            }
        });
});
});

Model: 模型:

    public function hotel_by_location($keyword){
        $this->db->like("location", $keyword);
        $query = $this->db->get('hotels');
        return $query->result_array();

Controller: 控制器:

    public function filter_hotel_by_loc(){
    $keyword=$this->input->post('loc');
    $data=$this->page_model->hotel_by_location($keyword);        
    echo json_encode($data);

View: 视图:

<div class="input-style-1 b-50 color-3">
<?php echo form_open_multipart('hotels/filter_hotel_by_loc') ?>
<img src="<?php echo base_url();?>assets/img/loc_icon_small_grey.png"alt="">
<input type="text" name="loc" placeholder="What city do you prefer?"id="loca" class="filter">
</form>
</div>

Please what am i doing wrong? 请问我做错了什么?

your input field of keyword have id loca but you used loc in keyword selector and post data must be in json format,so your ajax code should be like that 您的关键字输入字段具有id loca,但是您在关键字选择器中使用了loc ,并且发布数据必须采用json格式,因此您的ajax代码应类似于

$(document).ready(function () {
 $(".filter").keyup(function () {
    var keyword= $("#loca").val();
      $.ajax({
          type: "POST",
          url: CFG.url + '/pages/filter_hotel_by_loc/',
          data: {'keyword':keyword},
          dataType: "json",
          success: function (data) {
             console.log(data);
          }
       }); 
    });
});

and replace this line 并替换这条线

$keyword=$this->input->post('loc');

with this 有了这个

$keyword=$this->input->post('keyword');

and must check value of keyword which you get in post before pass it to model. 并且必须先检查您发布的关键字的值,然后再将其传递给模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM