-1

I have a form that i need to fill in order to fill out an allowance table. The allowance table contains Employee Code, Employee Name, Period of time, and days attended. The form only contains employee code, employee name, and period of time. So the days attended need to be filled automatically by counting on how many days attended from attendance table with condition based on the period of time.

This is the structure of the attendance table:

|  id_attend   |  employee_id  |  employee_name  |   date   |  time_in  |  time_out  |
|      1       |     RYN       |     RYAN        |01.01.2011|  07.00    |  19.00     |

Meanwhile this is the structure of allowance table

|  id_allowance   |  employee_id  |  employee_name  |   period  |  days_attended   |
|      1          |     RYN       |     RYAN        |  01.2011  |     1            |

So the days_attended should be filled by counting how many days attended based on employee_id and period. I tried this one in my model:

        public function count_days_attended()
    {
             $this->db->select('*, COUNT(attendance.date) as allowance.days_attended');
             $this->db->from('attendance');
             $this->db->join('allowance','attendance.employee_id= allowance.employee_id');
             $this->db->order_by('employee_id','DESC');
             $this->db->where(MONTH('attendance.date'), MONTH('allowance.period'));
             $query = $this->db->get();
             return $query->result();
    }

and this is my controller:

public function submit(){
    $dateTime = new DateTime($this->input->post('period'));
    $formatted_date = date_format($dateTime, 'Y-m-d' );

    $data = array(
        'employee_id'=>$this->input->post('employee_id'),
        'employee_name'=>$this->input->post('employee_name'),
        'period'=> $formatted_date,
    );
    $this->absengaji_m->count_days_attended;
    $this->absengaji_m->insert($data);
    redirect('absengaji');
   }

There is no error, but the program is not even give any response to my code. Once i clicked the submit its still show 0 on days attended

Is there any way to fix this? :/

Team
  • 55
  • 9
  • What exactly are you trying to fix? It sounds like it isn't working as you expect, but what symptoms are you seeing? Are errors being produced when the code is run? – amccormack Sep 12 '18 at 02:09
  • Oh geez i forgot to mention that my code is not working, it doesn't show any error but it doesn't give any response either @amccormack – Team Sep 12 '18 at 02:13
  • Does the redirect occur? Have you checked your logs for errors? – amccormack Sep 12 '18 at 02:18
  • Yes, the redirect still occur. It seems like nothing happened, working normally except the days attended is still 0. @amccormack – Team Sep 12 '18 at 02:20

1 Answers1

2

I think you have several issues at play here. To rattle them off,

  1. You are not specifying which employee in your WHERE clause
  2. You are not calling your method properly
  3. You are not capturing the return of your method
  4. You are not appending the result to your insert query.

I am no CodeIgniter developer, so I can't help you with specific syntax, but something like this should get you on the right track,

//accept a parameter so you can know which employee
public function count_days_attended( $employee_id ) {
     $this->db->select('*, COUNT(attendance.date) as allowance.days_attended');
     $this->db->from('attendance');
     $this->db->join('allowance','attendance.employee_id= allowance.employee_id');
     $this->db->order_by('employee_id','DESC');
     $this->db->where(MONTH('attendance.date'), MONTH('allowance.period'));
     //add the employee id to your where clause so you are searching for the correct employee only
     //$this->db->where()   employee id = $employee_id
     $query = $this->db->get();
     return $query->result();   //you are returning a result here
}

public function submit(){
    $dateTime = new DateTime($this->input->post('period'));
    $formatted_date = date_format($dateTime, 'Y-m-d' );

    $data = array(
        'employee_id'=>$this->input->post('employee_id'),
        'employee_name'=>$this->input->post('employee_name'),
        'period'=> $formatted_date,
    );

    //save the result in a variable
    $days_attended = $this->absengaji_m->count_days_attended( $data['employee_id'] );  //call as a method and pass the employee id to the method

    //append to insert query
    $data['days_attended'] = $days_attended;

    $this->absengaji_m->insert($data);
    redirect('absengaji');
}
Chris
  • 4,762
  • 3
  • 44
  • 79
  • Wow, thanks for the answer! Will try it very soon, hopefully this can work. Im still new as well in this kind of thing tho – Team Sep 12 '18 at 02:33
  • No problem. Once you solve those issues, you may run into others, but that is part of the debugging process. Just work through each one. `var_dump()` and `print_r()` can be your best friends. – Chris Sep 12 '18 at 02:35
  • hey, tried your answer and yes, it leads me to another problem. it says `Message: Array to string conversion` and `Message: Undefined property: AbsenGaji::$count_days_attended` . i was wondering, should i used the print_r() in the same function as $count_days_attended or should i make a new one? @Chris – Team Sep 12 '18 at 05:13