options”值,如下所示: $fetch_sub_categories1 = '{ label: "", valu-6ren">
gpt4 book ai didi

php - 使用 PHP 从 MySQL 表中获取值并在 jQuery 函数中将结果显示为 "option"

转载 作者:行者123 更新时间:2023-11-29 20:09:35 25 4
gpt4 key购买 nike

我面临以下问题:

1) 我使用 PHP MySQL 检索了“select->options”值,如下所示:

$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) 我有一个现成的文档监听器 jQuery 函数,我用它来显示上述结果,如下所示:

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

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

但是,浏览器会将整个变量 $fetch_sub_categories1 解释为字符串,并且不会显示变量中定义的选项。

结果如下:

enter image description here

任何建议都将受到高度赞赏。

最佳答案

删除 <?php ?> 周围的单引号标签:

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

它们就是 $fetch_sub_categories1 内容的原因被解释为字符串。

关于php - 使用 PHP 从 MySQL 表中获取值并在 jQuery 函数中将结果显示为 "option",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40212467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com