【问题标题】:append result arrays in php codeigniter using ajax使用ajax在php codeigniter中追加结果数组
【发布时间】:2016-03-22 11:09:51
【问题描述】:

下面是我在控制台得到的json结果。但是,将其附加到我的下拉列表中,仅显示物理、化学和测试科目。如何获得生物学也。

[
  [
    {"id":"1","subject_name":"Physics"},
    {"id":"2","subject_name":"Chemistry"},
    {"id":"9","subject_name":"test subject"}
  ],
  [
    {"id":"7","subject_name":"Biology"}
  ]
]

下面是我的ajax代码

$.ajax({
    type: 'POST',
    dataType:"json",
    // url:'<?php echo base_url()."SchoolAdmin/delete_electricUsage";?>',
    url: '<?php echo base_url('SchoolAdmin/getclass_id'); ?>',
    data: { class_std : class_std},
    success: function (result) {
        var listItems= "";
        listItems+= "<option value='" + 'select' + "'>" +'Select' + "</option>";
        for (var i = 0; i < result[0].length; i++){
            listItems+= "<option value='" + result[0][i].id+ "'>" + result[0][i].subject_name + "</option>";
        }
        $("#subject").html(listItems);
    }
});

下面是我的控制器功能,

public function getclass_id()
{
    $school_id = $this->session->userdata('school_id');
    $sess_id = $this->session->userdata('userid');
    $user_id = $this->session->userdata('user_id');
    if(($sess_id))
    { 
        $class_id=$this->security->xss_clean($this->input->post('class_std'));
        $this->load->model('School_Model');
        $cid=$this->School_Model->getclass_id($class_id,$school_id);

        foreach ($cid as $r)
        {
            $rr=$r->id;
            $data[]=array_merge($this->School_Model->getSubject($rr,$school_id));
        }
        echo json_encode($data);
    }
    else
    {
        redirect('admin');
    }
}

这是我的模型,

public function getclass_id($class_id,$school_id)
{
    return $this->db->select('id')->from('class_table')->where('standard',$class_id)->where('school_id',$school_id)->
    get()->result();
    //echo $this->db->last_query();
}

public function getSubject($cid,$school_id)
{
    return $this->db->select('id,subject_name')->
    from('subject')->where('school_id',$school_id)->
    where('class_id',$cid)->get()->result_array();
}

【问题讨论】:

    标签: php json ajax codeigniter


    【解决方案1】:

    您的result 是一个数组数组。您需要循环所有内容!

    Biologyresult[1] 中,但你只是在循环result[0]

    可以手动完成,如下所示:

    for (var i = 0; i < result[0].length; i++){
        listItems+= "<option value='" + result[0][i].id+ "'>" + result[0][i].subject_name + "</option>";
    }
    for (var i = 0; i < result[1].length; i++){
        listItems+= "<option value='" + result[1][i].id+ "'>" + result[1][i].subject_name + "</option>";
    }
    

    或者,一口气:

    for (var j = 0; j < result.length; j++){ 
        // loops through all result arrays;
        for (var i = 0; i < result[j].length; i++){
            // loops the `j`th result for entries;
            listItems+= "<option value='" + result[j][i].id+ "'>" + result[j][i].subject_name + "</option>";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多