使用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.

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.