dongyoufo5672 2016-10-24 07:03
浏览 54
已采纳

使用PHP从MySQL表中获取值并在jQuery函数中显示结果作为“选项”

I am facing an issue as regards the following:

1) I have retrieved "select->options" values using PHP MySQL as below:

$fetch_sub_categories1 =    '{ label: "", value: "" },';
$fetch_sub_categories2 =    '{ label: "", value: "" },';
$fetch_sub_categories3 =    '{ label: "", value: "" },';
$fetch_sub_categories4 =    '{ label: "", value: "" },';


$num_of_returned_rows = mysqli_num_rows($get_sub_cats_query);        
if ($num_of_returned_rows>0)
{
for ($i=0;$i<$num_of_returned_rows;$i++)
{
     $row=mysqli_fetch_array($get_sub_cats_query);

     if ($row['category'] == 'Αρχικά Πιάτα'){
        if($i==$num_of_returned_rows) $fetch_sub_categories1 .= '{ label: ""'.$row['sub_category'].'"", value: ""'.$row['sub_category'].'"" }';
        else                          $fetch_sub_categories1 .= '{ label: ""'.$row['sub_category'].'"", value: ""'.$row['sub_category'].'"" },';
     }
     if ($row['category'] == 'Κυρίως Πιάτα') {
        if($i==$num_of_returned_rows) $fetch_sub_categories2 .= '{ label: ""'.$row['sub_category'].'"", value: ""'.$row['sub_category'].'"" }';
        else                          $fetch_sub_categories2 .= '{ label: ""'.$row['sub_category'].'"", value: ""'.$row['sub_category'].'"" },';
     }
     if ($row['category'] == 'Επιδόρπια')   {
        if($i==$num_of_returned_rows) $fetch_sub_categories3 .= '{ label: ""'.$row['sub_category'].'"", value: ""'.$row['sub_category'].'"" }';
        else                          $fetch_sub_categories3 .= '{ label: ""'.$row['sub_category'].'"", value: ""'.$row['sub_category'].'"" },';
     }
     if ($row['category'] == 'Ποτά')         {
        if($i==$num_of_returned_rows) $fetch_sub_categories4 .= '{ label: ""'.$row['sub_category'].'"", value: ""'.$row['sub_category'].'"" }';
        else                          $fetch_sub_categories4 .= '{ label: ""'.$row['sub_category'].'"", value: ""'.$row['sub_category'].'"" },';
     }
}

}

2) I have a ready document listener jQuery function which I use to display the above results as below:

label: "Υποκατηγορία:",
                                name: "sub_category",
                                type:  "select",
                                options: [

                                        '<?php echo $fetch_sub_categories1; ?>'
                                ]

However the browser interprets the whole variable $fetch_sub_categories1 as a string and does not display the options as defined within the variable.

The result as below:

enter image description here

Any advice will be highly appreciated.

  • 写回答

1条回答 默认 最新

  • duandushang5148 2016-10-24 07:55
    关注

    Remove the single quotes around the <?php ?> tag:

    label: "Υποκατηγορία:",
    name: "sub_category",
    type: "select",
    options: [
        <?php echo $fetch_sub_categories1; ?>
    ]
    

    They are the reason why the content of $fetch_sub_categories1 is interpreted as a string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置