简体   繁体   English

我应该如何在下拉列表中显示多个项目?

[英]how I should display the multiple item in the dropdown?

I want to display some multiple valus in dropdown with autocomplete我想通过自动完成在下拉列表中显示一些多个值

$(document).ready(function () {
    $('#Client-ville_id').typeahead({
        source: function (query, result) {
            $.ajax({
                url: "list_ville.php",
                data: 'query=' + query,
                dataType: "json",
                type: "POST",
                success: function (data) {
                    result($.map(data, function (item) {
                        return {
                            name: item.name,
                            cp:item.departement_code
                        }
                    }));
                }
            });
        }
    });
});
$sql = $conn->prepare("SELECT * FROM ville WHERE name LIKE ?");
$sql->bind_param("s",$search_param);
$sql->execute();
$result = $sql->get_result();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $countryResult[] = $row["name"];
        $countryResult[] = $row["departement_code"];
    }
    echo json_encode($countryResult);
}
$conn->close();

But I get an error但我收到一个错误

Uncaught TypeError: Cannot read property 'name' of null at (index):531未捕获的类型错误:无法读取 (index):531 处的 null 属性“名称”

The problem is that you're creating array with strings in php, you need问题是你在 php 中用字符串创建数组,你需要

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $countryResult[] = array(
          "name" => $row["name"],
          "departement_code" => $row["departement_code"]
        );
    }
    echo json_encode($countryResult);
}

or just:要不就:

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $countryResult[] = $row;
    }
    echo json_encode($countryResult);
}

there is function that fetch all data in one function call, here is one for mysqli: mysqli_result::fetch_all .有一个函数可以在一个函数调用中获取所有数据,这里是 mysqli 的一个: mysqli_result::fetch_all

In your sql file do在你的 sql 文件中做

$sql = $conn->prepare("SELECT * FROM ville WHERE name LIKE ?");
$sql->bind_param("s",$search_param);
$sql->execute();
$result = $sql->get_result();
if ($result->num_rows > 0) {
$i = 0;
    while($row = $result->fetch_assoc()) {
        $countryResult[$i]['name'] = $row["name"];
        $countryResult[$i]['departement_code'] = $row["departement_code"];
        $i++;
    }
    echo json_encode($countryResult);
}
$conn->close();

The problem is that you create a non associative array, but reffer to it as associative.问题是您创建了一个非关联数组,但将其视为关联数组。

while($row = $result->fetch_assoc()) {
    $countryResult[] = $row["name"];
    $countryResult[] = $row["departement_code"];
}

After this loop $countryResult array would like something like this在这个循环之后$countryResult数组会像这样

array(6) {
  [0] => "name1"
  [1] => "departement_code1"
  [2] => "name2"
  [3] => "departement_code2"
  [4] => "name3"
  [5] => "departement_code3"
}

You should change the while loop:您应该更改while循环:

while($row = $result->fetch_assoc()) {
    $countryResult[] = $row;
}

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

相关问题 如何在下拉列表中仅显示多个项目中的一个项目 - How to display only one item each of multiple items in a dropdown 在多条件下拉过滤器中,如何显示结果内容? - In a multiple criteria dropdown filter, how do I display resulting content? 如何显示下拉列表以及在JQgrid中添加项目的选项 - How to display Dropdown with option to Add item in JQgrid 如何从Bootstrap下拉菜单中显示所选项目? - How to display selected item from Bootstrap dropdown? 如何在dropDown上显示所选列表项的文本? - How to display text of selected List item on dropDown? 当我将鼠标悬停在下拉列表中的某个项目上时,如何显示JQuery工具提示? - How can I get the JQuery Tooltip to display when I hover over an item on a dropdown list? 我创建了一个下拉列表,如何显示所选项目的其他列数据? - I created a dropdown list and how to display the other columns data of selected item? 如何显示从下拉列表中选择的项目不应出现在复选框列表中 - How to show item selected from dropdown should not appear in checkbox list 如何显示在下拉列表中选择的多个值 - How to display multiple value selected in dropdown 如何仅显示多个下拉值数组中的1个? - How to display only 1 of the array of multiple dropdown value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM