如何将选择下拉菜单与 json 一起使用


How to use select drop menu with json

我想有一个带有此脚本的选择下拉菜单:链接

我有一个脚本已经让我有了 json 格式的数据,我想使用相同的脚本来填充 2 个菜单。例如,我想有 2 个菜单,它们将具有drivercars

这是脚本:

echo '{ "results" : [ ';
$gettruck_result = dbMySql::Exec(.........query.........);
$result_array = array();
while($row = mysql_fetch_assoc($gettruck_result)){
  $row_object = '{';
  $row_object .= '"id": "' . $row['id'] . '", ';
  $row_object .= '"driver": "' . $row['drivers'] . '", ';
  $row_object .= '"trucks": "' . $row['trucks'] . '", ';
  $row_object .= '}';    
  $result_array[] = $row_object;
}
$result_str = implode(", ", $result_array);
echo $result_str;
echo " ] }";
?>

这是他们在文档中的内容:

[{text:'Lorem', value:'5', title:'icons/enabled.gif'},{text:'Ipsum', value:'6',title:'icons/enabled.gif'}]

我想要这样的东西:1.当您加载驱动程序时:text应该是驱动程序的名称,value应该是id,title将是一些图片2.装载卡车时:text应该是卡车名称,value应该是id,title是不同的图标。

是否可以在仅调用一次脚本的同时执行此操作?

你知道有一个php函数可以将对象/数组编码为json吗?

json_encode http://www.php.net/json_encode

我认为最简单的方法是:1) 提供一个嵌套的 json 结构,如下所示:

drivers {
    text: 'Lorem',
    value: '5',
    title: 'icons/enabled.gif',
    trucks: {
        text: 'Ipsum',
        value: '4',
        title: 'icons/foo.gif',
...

2) 另一种选择是在用户选择驱动程序时进行 Ajax 调用。此 ajax 调用的返回将保留该特定驱动程序的所有卡车。