【问题标题】:Filtering JQuery Autoncomplete Results in PHP在 PHP 中过滤 JQuery 自动完成结果
【发布时间】:2014-05-09 22:49:20
【问题描述】:

我正在尝试让我的 PHP 脚本根据用户的输入将 CSV 表转换为 JSON 数组以用于我的 JQuery 自动完成字段过滤器结果。这是我到目前为止的 PHP 代码:

<?php
$file="partstable.csv";
$csv= file_get_contents($file);
$input = $_GET["term"];
$lines = explode("\n", $csv);
$head = str_getcsv(array_shift($lines));
$array = array();
foreach ($lines as $line) {
    $array[] = array_combine($head, str_getcsv($line));
}
$result = array_filter($array, function ($item) use ($input) {
    if (stripos($item, $input) !== false) {
        return true;
    }
    return false;
});
print json_encode($result);
?>

CSV:

label,Number,PartDesc,Source
"First Part",10002345,This is a description of the part,1
"Second Part",10006789,This is also a part description,1
.....

jQuery:

$(function() {      
    $('#partName').autocomplete({
        minLength: 2,
        source: "readcsv.php",
        select: function ( event, ui ) {
        $("#partNumber").text(ui.item.Number);
        $("#partDesc").text(ui.item.PartDesc);
            if (ui.item.Source == 1 && document.getElementById('radio1').checked) {
                $("#partSource").html("<div class='ui-widget'><div class = 'ui-state-highlight ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: 6px;'></span> TEXT</p></div></div>");
            }
            else if (ui.item.Source == 1 && document.getElementById('radio2').checked) {
                $("#partSource").html("<div class='ui-widget'><div class = 'ui-state-highlight ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: 6px;'></span> TEXT </p></div></div>");
            }
            else if (ui.item.Source == 2) {
                $("#partSource").html("<div class='ui-widget'><div class = 'ui-state-highlight ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: 6px;'></span> TEXT</p></div></div>");
            }
            else {
                $("#partSource").html("<div class='ui-widget'><div class = 'ui-state-highlight ui-corner-all' style='margin=top:5px;'><p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: 6px;'></span> TEXT</p></div></div>");
            }
      }
    });
})

但是,这仍然会导致自动完成文本框为我提供数组中的所有标签。我知道我的搜索页面正在正确地将搜索字符串传递给我的 PHP 脚本,所以问题出在这个脚本的某个地方。如果我理解我的代码,它应该逐行遍历我的数组,只返回标签开始包含$input 中输入的搜索字符串,对吗?

【问题讨论】:

  • $array 的每个项目本身都是一个数组,但您尝试在此数组中找到 $input - 不是在它的特定项目中。你确定你显示的代码没有遗漏吗?
  • @raina77ow 我发布的代码是整个 .php 文件,所以我错过了搜索应该执行的步骤。所以我只需要搜索每个组件数组内部的标签字符串的内容;只需要确定如何去做。

标签: php jquery arrays json autocomplete


【解决方案1】:

编辑:

使用这个(我已经测试过了):

$result = array_filter($array, function ($item) use ($input) {
    foreach($item as $field){
        if(stripos($field, $input, 0) !== false) return true;
    }
    return false;
});

【讨论】:

  • 使用 multineedle_stripos 似乎在我的服务器上导致错误 500;我正在尝试确定这是服务器执行我的脚本的问题,还是导致问题的数组过滤器。该脚本在将 stripos 替换为 multineedle_stripos 之前正在运行
  • 编辑:我的错误;我需要在我的代码中定义 multineedle_stripos。这解决了错误 500,但它仍然没有过滤我的结果......
  • 能否发布您自己的 csv 示例以更好地调试它?
  • 将 .csv 添加到我的原始帖子中。很抱歉没有事先添加。
  • 查看我的新答案,正如@raina77ow 指出的那样,给函数一个数组时出错,所以它总是返回与(boolean)false 不同的东西,所以...
猜你喜欢
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 2016-05-26
  • 1970-01-01
  • 2023-03-13
  • 2011-01-04
  • 1970-01-01
  • 2014-01-19
相关资源
最近更新 更多