亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用于填充 HTML 下拉列表的 PHP 查詢響應(通過二維數組循環)

用于填充 HTML 下拉列表的 PHP 查詢響應(通過二維數組循環)

泛舟湖上清波郎朗 2023-05-11 10:19:56
我從我的 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>我在這里做錯了什么?或丟失/遺忘?
查看完整描述

1 回答

?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

考慮以下 PHP。


public function process($which) {

  if(isset($which)){

    if ($which == 'a_table1'){

      $sql = "SELECT a_table1 FROM data.blah1";

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

      $sql = "SELECT a_table2 FROM data.blah2";

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

      $sql = "SELECT a_table3 FROM data.blah3"; 

    }

 

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

    oci_execute($stid); 

        

    if (!@oci_execute($stid)) {

      $error = oci_error($stid);

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

    }

 

    $myData = array();

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

      array_push($myData, $list);

    }

 

    header('Content-Type: application/json');

    echo json_encode($myData);

 

  } else {

    header('Content-Type: application/json');

    echo json_encode(array("error" => "WHICH not assigned"));

  }

}

然后這應該發回數組的 JSON 數據。


例子


[{

  "A_ID":"OJC-FCT"

},{

  "A_ID":"DAL-ATCT"

},{

  "A_ID":"AFF-MIL-TWR"

},{

  "A_ID":"CNO-ATCT"

},{

  "A_ID":"GSN-FCT"

},{

  "A_ID":"CGI-NFCT"

},{

  "A_ID":"NDZ-MIL-TWR"

},{

  "A_ID":"FCS-MIL-TWR"

},{ 

  "A_ID":"LAL-FCT"

},{

  "A_ID":"LNK-ATCT"

},{

  "A_ID":"CHD-FCT"

},{

  "A_ID":"FLG-FCT"

},{

  "A_ID":"MCN-FCT"

},{

  "A_ID":"SKA-MIL-TWR"

}];

然后,您可以在每個循環中使用它來構建選項。


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

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

  console.log(response);

  $("#agent").html("");

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

    $("<option>").html(value['A_ID']).appendTo($("#agent"));

  });

});

您還可以簡化 PHP 輸出,使其只是項目的結果數組。


$myData = array();

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

  array_push($myData, $list['A_ID']);

}

那么你的循環也會被簡化。


$("#agent").html("");

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

  $("<option>").html(value).appendTo($("#agent"));

});


查看完整回答
反對 回復 2023-05-11
  • 1 回答
  • 0 關注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號