【问题标题】:not getting all rows from the database没有从数据库中获取所有行
【发布时间】:2016-09-14 23:32:13
【问题描述】:

我在 codeigniter 中开发了一个自动完成功能,但问题是,如果我们输入两个字母,它会匹配 2 个或超过 2 个字符,但它只显示 1 条记录,我不知道为什么会这样。谁能帮帮我。

我有 3 条符合以下条件的记录:

  • 车辆
  • 重型道路车辆
  • 小型车辆

当我输入 Ve 时,它会显示 Design & Development 而当我输入 Beh 时,它只会显示 Vehicle 因为它应该显示所有三个。所有 3 条记录都包含名称 vehicles,谁能告诉我我在哪里做错了

这是我的 php 模型 Get_data.php

public function result($keyword) {
        $get_data = $this->db->like('cat_name', $keyword);
        $get_data = $this->db->order_by("cat_id", "ASC"); 
        $get_data = $this->db->get('categories');

        foreach($get_data->result() as $data):
            $sport = str_replace($keyword, "<b>" . $keyword . "</b>" , $data->cat_name);
            $html  = '<li onclick="set_item(\''.str_replace("'", "\'", $data->cat_name).'\')">';
            $html .= $sport;
            $html .= "</li>";
            return $html;
        endforeach;
    }

我的 php 控制器

public function getData() {
        $keyword = $this->input->post('keyword');
        $html = $this->get_data->result($keyword);
        echo $html;
    }

这是我使用的 ajax 查询

function autocomp() {
var min_length = 0;
var keyword    = $("#skills").val();

if(keyword.length > min_length) {
    $.ajax({
        type     :  "POST",
        data     :  {keyword: keyword},
        dataType :  'text',
        url      :  "http://localhost:90/tufuturo/home/getData",
        success  :  function(data) {
                        $("#resultkeyword").show();
                        if(data == false) {
                            $("#resultkeyword").html("<li>No Results Found</li>");
                        } else {
                            $("#resultkeyword").html(data);
                        }
                    }
    });
} else {
    $("#resultkeyword").hide();
}
}

function set_item(item) {
    $("#skills").val(item);
    $("#resultkeyword").html("<li><b>"+item+"</b></li>");
    $("#resultkeyword").hide();
}

【问题讨论】:

    标签: php jquery mysql ajax codeigniter


    【解决方案1】:

    仅更新 foreach 循环

    $html = '';
    foreach($get_data->result() as $data):
                $sport = str_replace($keyword, "<b>" . $keyword . "</b>" , $data->cat_name);
                $html  .= '<li onclick="set_item(\''.str_replace("'", "\'", $data->cat_name).'\')">';
                $html .= $sport;
                $html .= "</li>";
                
            endforeach;
    return $html;

    【讨论】:

    • 太棒了,你能指出我的错误吗?我在想我只返回 li ,这显然会返回最后一个值,那么我在哪里做错了什么?
    • - 您正在循环中将 li 分配给 $html,因此它每次都会启动,并且您将从循环返回,该循环仅返回结果集中的第一个元素
    • 完美,所以如果我必须返回循环的所有数据,我必须在循环外开始分配变量
    【解决方案2】:

    返回类型后一切都会消失。您在 foreach 循环中编写 return 。循环只迭代一次并返回。

    需要将li的开始标签与$html连接起来

    也以

    的形式在 foreach 循环之外返回
    foreach($get_data->result() as $data):
                $sport = str_replace($keyword, "<b>" . $keyword . "</b>" , $data->cat_name);
                $html  .= '<li onclick="set_item(\''.str_replace("'", "\'", $data->cat_name).'\')">';
                $html .= $sport;
                $html .= "</li>";
    
            endforeach;
    
    return $html;// return here
    

    使用num_rows();查看查询返回结果与否

    $num = $get_data->num_rows();
    
    if($num>0)
    {
    
     foreach($get_data->result() as $data):
                    $sport = str_replace($keyword, "<b>" . $keyword . "</b>" , $data->cat_name);
                    $html  .= '<li onclick="set_item(\''.str_replace("'", "\'", $data->cat_name).'\')">';
                    $html .= $sport;
                    $html .= "</li>";
    
                endforeach;
    }else{
         $html="<li>NO result found</li>";
    }
    
        return $html;// return here
    

    【讨论】:

    • 虽然您在循环之外返回,但它会返回一个值,因为在您将“li”分配给 $html 的代码中。所以每次 $html 都会启动,它会返回最后一个元素作为输出
    • 当不匹配任何东西时,它会得到一个错误未定义的变量返回,并且只显示 1 个结果
    • @MarkAlan 使用num_rows检查答案
    • 好吧,我的错误是我只输出了 li,这意味着我只会得到最后一个值吗?
    • 您是否在= $html = '&lt;li 之前添加. ??
    【解决方案3】:

    尝试给 LIKE 命令提供第三个参数,让您将通配符 % 放在您想要的位置。我建议在第三个参数中使用 'after' 更改此行 $get_data = $this->db->like('cat_name', $keyword); 到

    $get_data = $this->db->like('cat_name', $keyword,'after');
    

    【讨论】:

    • 当我输入 Ve 时仍然相同,它向我显示了 Vehicles 但只有 1 条记录没有其他记录我在循环中返回内部 li 是否影响我相信循环没有返回给我跨度>
    猜你喜欢
    • 2016-06-28
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2017-04-03
    相关资源
    最近更新 更多