0

The below is the json result I got in the console. But, appending it to my dropdown, shows only Physics,Chemistry and test subject. How to get Biology also.

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

Below is my ajax code

$.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);
    }
});

Below is my controller function,

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');
    }
}

Here is my model,

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();
}

1 Answer 1

2

Your result is an array of arrays. You need to loop everything!

Biology is in result[1], but you're only looping result[0]

You could do it manually, like this:

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>";
}

Or, in one go:

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>";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.