Search code examples
phpjqueryjsonajaxcodeigniter-3

Getting twice output in JSON


I am using CodeIgniter, I have a form and fields are Employee_name, fromDate, and endDate. I am sending the data to Ajax.(I haven't shared the form code and model code) Now I am displaying the records from database using AJAX and JSON. I am getting the correct output from the database but there is some issue I don't know it's JSON or another issue. I am getting the undefined and then getting my output.

In below image, I am getting the total 9 records from the database which is correct but when I am displaying using JSON then I am getting 18 records. First 9 records undefined and next 9 records my output.

Would you help me out in this issue?

enter image description here View

<!--form code here-->
<form name="employee_attendance"></form>
<!--end form code here-->

$("form[name='employee_attendance']").validate({
  rules: {
    employee_Name: {
      required: true
    },
    fromDate: {
      required: true
    },
    toDate: {
      required: true
    }
  },
  submitHandler: function(form) {
    var employee_Name = $('#employee_Name').val();
    var fromDate = $('#fromDate').val();
    var toDate = $('#toDate').val();                           
    $.ajax({
      url: baseUrl + "/Reports_control/report_attendance",
      method: "POST",
      //dataType: "json", 
      data: {
        employee_Name: employee_Name,
        fromDate: fromDate,
        toDate: toDate
      },
      success: function(response) {
        $('.search_record tbody tr').hide();
        var data = JSON.parse(response);
        if (data.status === 'error') {
          alert(data.msg);
        }
        if (data.status === 'success') {
          $('.addendence_report_list').show();
          var trHTML = '';
          $.each(data.records, function(i, o) {
            trHTML += '<tr><td>' + o.Sr_no +
              '</td><td>' + o.name +
              '</td><td>' + o.employee_id +
              '</td><td>' + o.last_activity +
              '</td><td>' + o.total_days +
              '</td></tr>';
          });
          $('.search_record tbody').append(trHTML);
        }
      }
    });
  }
});

Controller

public function report_attendance()
{
   $employee_id=trim($this->input->post('employee_Name'));
   $fromDate=trim(date('Y-m-d', strtotime($this->input->post('fromDate'))));
   $toDate=trim(date('Y-m-d', strtotime($this->input->post('toDate'))));
   if((!empty($employee_id)) && (!empty($fromDate)) && (!empty($toDate))){
     $result=$this->Reports_model->get_employee_attendance($employee_id,$fromDate,$toDate); 
    }
    if (empty($result) || $result == 0){
    $arr_result['status'] = "error";
    $arr_result['msg'] = "No record found";
    }
 else 
    {
        $n=1;
      foreach ($result as $row)
        {
        $result[] = array(
              "Sr_no" => $n,
              "name" => $row->firstname.' '.$row->lastname,
              "employee_id" => $row->employee_id,
              "last_activity"=>$row->login_time,
              "total_days"=>$row->total_days
            );
        $n++;
          }
    $arr_result['status'] = 'success';
    $arr_result['records'] = $result;
        }
      echo json_encode($arr_result);
       exit;
}

view

<div class="search_record">
  <table cellspacing="0" id="attendence_report_list">
    <thead>
      <tr>
        <th class="" width="5%">Sr. No.</th>
        <th class="" width="11%">Name</th>
        <th class="" width="11%">Emp Id</th>
        <th class="" width="9%">Date </th>
        <th class="" width="9%">Working hours</th>
        <th class="" width="9%">Leave Days</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <tr>
        </tr>
    </tbody>
  </table>
</div>

Solution

  • Change the name of the array in foreach from $result[] to results[] and check it.