使用一个 php 函数返回多个 json 对象

阿兹

我正在尝试填充两个下拉菜单,其值取决于另一个下拉菜单。我总共有 3 个下拉菜单。一个用于选择课程,另外两个用于选择所选课程的科目和考试。我可以填充一个下拉菜单(例如:选择主题)。但是我如何才能填充它们。我的代码如下:

  <table>
       <tr>
          <td> select class</td>
       </tr>
       <tr>
          <td> select subject</td>
       </tr>
         <tr>
          <td> select exam</td>
       </tr>
       <tr>
       <td><select class="form-control m-bot15" name="class_id" value='' onchange="myFunction(this)"  style="float:left;" id="carId">
            <option value="">select a class</option>
             <option value="1">One</option>
             <option value="2">Two</option>
             <option value="3">Three</option>
            </select></td>
         </tr>
         <tr>
         <td><select class="form-control m-bot15" name="subject_id" value='' style="float:left;" id="emptyDropdown">
            <option value="">select a subject</option>
            </select></td>
          <tr>
         </tr>
         <td><select class="form-control m-bot15" name="exam_id" value='' style="float:left;" id="emptyDropdown2">
            <option value="">select a subject</option>
            </select></td>
          </tr>
         </table>

这是我的视图函数 我用于填充主题名称的脚本是:

  <script>
        function myFunction(obj)
            {
              $('#emptyDropdown').empty()
              var dropDown = document.getElementById("carId");
              var carId = dropDown.options[dropDown.selectedIndex].value;
              $.ajax({
                     type: "POST",
                      dataType: 'json',
                      url: "<?php echo base_url();?>mark/newdrp",
                      data: { 'carId': carId  },
                      success: function(data){

                          $.each(data, function () {
                          $('#emptyDropdown').append('<option value='+this.subject_id+'>' + this.name + '</option>');
                      });
                   }
                });
               }
         </script>

我的控制器功能 newdrp 是

 public function newdrp(){
        $classId = $this->input->post('carId');
        $subjects = $this->subject_model->Subjectbyclassid($classId);
        echo json_encode($exams);
  }

这工作正常。我正在我的下拉列表中获取主题列表。但我想再传递一个这样的 json 对象

     public function newdrp(){
        $classId = $this->input->post('carId');
        $subjects = $this->subject_model->Subjectbyclassid($classId);
        $exams = $this->exam_model->Exambyclassid($classId);
        echo json_encode(array($subjects,$exams));
  }

这是我的控制台预览

    [[{"subject_id":"1","name":"Physics","class_id":"1","teacher_id":null},{"subject_id":"2","name":"Chemistry","class_id":"1","teacher_id":null},{"subject_id":"3","name":"Mathematics","class_id":"1","teacher_id":null}],[{"exam_id":"22","name":"BW9","date":"03\/10\/16","class_id":"1","comment":""},{"exam_id":"26","name":"BW10","date":"17\/10\/16","class_id":"1","comment":""},{"exam_id":"30","name":"BW11","date":"31\/10\/16","class_id":"1","comment":""},{"exam_id":"34","name":"BW12","date":"14\/11\/16","class_id":"1","comment":""},{"exam_id":"40","name":"BW13","date":"28\/11\/16","class_id":"1","comment":""},{"exam_id":"45","name":"BW14","date":"11\/12\/16","class_id":"1","comment":""},{"exam_id":"46","name":"Revision Exam 1","date":"02\/01\/17","class_id":"1","comment":""},{"exam_id":"49","name":"Revision Exam 2","date":"8\/01\/2017","class_id":"1","comment":""},{"exam_id":"51","name":"Revision Exam 3","date":"15\/01\/17","class_id":"1","comment":""},{"exam_id":"55","name":"Revision Exam 4","date":"22\/01\/17","class_id":"1","comment":""},{"exam_id":"57","name":"Revision exam 5","date":"26\/01\/2017","class_id":"1","comment":""},{"exam_id":"59","name":"Revision Exam 6","date":"29\/01\/17","class_id":"1","comment":""}]]

我怎样才能遍历这个并在相应的下拉列表中显示考试名称。请帮忙

丹·菲利普

您还必须遍历第二个数组,data作为父数组

success: function(data){
  var examArr = data[1];
  $.each(examArr, function () {
    $('#emptyDropdown2').append("<option value='" + $(this).exam_id + "'>" + $(this).name + "</option>");
  });
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章