用于填充 HTML 下拉列表的 PHP 查询响应(通过二维数组循环)

我从我的 PHP 代码和查询中得到了我需要的东西;除了我真的很难将数据带到前端以填充 HTML 下拉列表。

这是我在 PHP 方面的内容;一切正常

$app->get('/dlteopt', function ($request, $response, $args) {   

        

    $which = $_GET['id'];



    if ($which) {


        if ($which == 'table_1'){

            $sql = "SELECT item1 FROM daya.blahblah";

        } else if ($which == 'table_2'){

            $sql = "SELECT item2 FROM daya.blahblah2";

        } else if ($which == 'table_3'){

            $sql = "SELECT item3 FROM daya.blahblah3"; 

        }


        $stid = oci_parse($this->db, $sql);        


        $list = array();


        while ($list = oci_fetch_array($stid, OCI_ASSOC)) {

            $list[] = $list;

            var_dump($list); // this outputs the correct array I need, but cant bring it to front correctly into dropdown

        }



        if (!@oci_execute($stid)) {

            $error = oci_error($stid);


            throw new Exception($error['message']);

        } 


        oci_execute($stid);


    }

这是 jQuery;控制台response日志只是which我随get请求发送的标志 ( ) 变量,它确定通过用户场景查询哪个表。我需要的数组被省略了......


 let which = $(frm).attr("id");


    $.get('dlteopt', {id: which }, function (response) { 


       console.log(response); // this just consoles as the $which var no array


       $.each(response, function(index, value) {

         // started logic to append values in option; but no array or obj found/brought in to iterate through, can handle this part if can get array

       });


    });

html下拉;只是带有占位符的标准 HTML 选择,直到填充:


<select name='agent' id='agent'><option>Loading...</option></select>

我在这里做错了什么?或丢失/遗忘?


泛舟湖上清波郎朗
浏览 165回答 1
1回答

长风秋雁

考虑以下 PHP。public function process($which) {&nbsp; if(isset($which)){&nbsp; &nbsp; if ($which == 'a_table1'){&nbsp; &nbsp; &nbsp; $sql = "SELECT a_table1 FROM data.blah1";&nbsp; &nbsp; } else if ($which == 'a_table2'){&nbsp; &nbsp; &nbsp; $sql = "SELECT a_table2 FROM data.blah2";&nbsp; &nbsp; } else if ($which == 'a_table3'){&nbsp; &nbsp; &nbsp; $sql = "SELECT a_table3 FROM data.blah3";&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; $stid = oci_parse($this->db, $sql);&nbsp; &nbsp; oci_execute($stid);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if (!@oci_execute($stid)) {&nbsp; &nbsp; &nbsp; $error = oci_error($stid);&nbsp; &nbsp; &nbsp; throw new Exception($error['message']);&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; $myData = array();&nbsp; &nbsp; while ($list = oci_fetch_array($stid, OCI_ASSOC)) {&nbsp; &nbsp; &nbsp; array_push($myData, $list);&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; header('Content-Type: application/json');&nbsp; &nbsp; echo json_encode($myData);&nbsp;&nbsp; } else {&nbsp; &nbsp; header('Content-Type: application/json');&nbsp; &nbsp; echo json_encode(array("error" => "WHICH not assigned"));&nbsp; }}然后这应该发回数组的 JSON 数据。例子[{&nbsp; "A_ID":"OJC-FCT"},{&nbsp; "A_ID":"DAL-ATCT"},{&nbsp; "A_ID":"AFF-MIL-TWR"},{&nbsp; "A_ID":"CNO-ATCT"},{&nbsp; "A_ID":"GSN-FCT"},{&nbsp; "A_ID":"CGI-NFCT"},{&nbsp; "A_ID":"NDZ-MIL-TWR"},{&nbsp; "A_ID":"FCS-MIL-TWR"},{&nbsp;&nbsp; "A_ID":"LAL-FCT"},{&nbsp; "A_ID":"LNK-ATCT"},{&nbsp; "A_ID":"CHD-FCT"},{&nbsp; "A_ID":"FLG-FCT"},{&nbsp; "A_ID":"MCN-FCT"},{&nbsp; "A_ID":"SKA-MIL-TWR"}];然后,您可以在每个循环中使用它来构建选项。var which = $(frm).attr("id");$.get('dlteopt', {id: which }, function (response) {&nbsp;&nbsp; console.log(response);&nbsp; $("#agent").html("");&nbsp; $.each(response, function(index, value) {&nbsp; &nbsp; $("<option>").html(value['A_ID']).appendTo($("#agent"));&nbsp; });});您还可以简化 PHP 输出,使其只是项目的结果数组。$myData = array();while ($list = oci_fetch_array($stid, OCI_ASSOC)) {&nbsp; array_push($myData, $list['A_ID']);}那么你的循环也会被简化。$("#agent").html("");$.each(response, function(index, value) {&nbsp; $("<option>").html(value).appendTo($("#agent"));});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript