jeudi 31 décembre 2015

database update using switchery switch using ajax in codeigniter

I am developing an attendance management codeigniter app and i am using switchery switch plugin to update attendance status. so the process will be:

  1. system display the list of student with attendance status in checkbox with switchery plugin
  2. using check or uncheck the checkbox with switchery plugin, if the student is present or absent or simply user change the status.
  3. system sends the form using ajax, if the student present 1 or absent 0, with student ID and attendance date
  4. and simply system will change the attendance status of student
  5. NOTE* THE VALUE OF $date $month $year $program_id $batch_id and $section_id is loaded before in controller function parameter.

and this my app's preview... i need to this form working using ajax preview of attendance app with switchery plugin

and this is my code for the view.

<!-- student attendence status update form -->
<form method="post" action="<?php echo base_url();?>index.php?admin/student_attendance/<?php echo $date.'/'.$month.'/'.$year.'/'.$program_id.'/'.$batch_id;?>">
  <!-- start datatable -->
  <table class="table table-hover dataTable table-striped width-full" id="departmentTableTools">
    <thead>
      <tr>
        <th class="text-center"><?php echo get_phrase('rool'); ?></th>
        <th class="text-center"><?php echo get_phrase('name'); ?></th>
        <th class="text-center"><?php echo get_phrase('status'); ?></th>
      </tr>
    </thead>
    <tbody>
      <?php $students = $this->db->select('*')
->from('student as tbl1, student_acedemic_process as tbl2')
->where('tbl1.student_id = tbl2.student_id')
->where('tbl2.program_id ='.$program_id)
->where('tbl2.batch_id ='.$batch_id)
->where('tbl2.status = 1')
->get()->result_array();
foreach($students as $row):?>
      <tr>
        <td class="text-center"><?php echo $row['roll_no'];?></td>
        <td class="text-center"><?php echo $row['name'];?></td>
        <td class="text-center">
          <?php 
//inserting blank data for students attendance if unavailable
$verify_data  = array(  'student_id' => $row['student_id'],
'date' => $full_date);
$query = $this->db->get_where('attendance' , $verify_data);
if($query->num_rows() < 1)
$this->db->insert('attendance' , $verify_data);

//showing the attendance status editing option
$attendance = $this->db->get_where('attendance' , $verify_data)->row();
$status   = $attendance->status;
?>

<input type="checkbox" value="1" id="inputBasicOn" name="status_<?php echo $row['student_id'];?>" 
data-plugin="switchery" <?php if($status == 1) echo 'checked'; ?> />
        </td>
      </tr>
      <?php endforeach;?>
    </tbody>
  </table>
  <input type="hidden" name="date" value="<?php echo $full_date;?>" />
  <center>
    <input type="submit" class="btn btn-info" value="save changes">
  </center>
</form>

and this my controller code to update the database

if($_POST)
{
// Loop all the students of $program_id, $batch_id, $section_id

$students = $this->db->select('*')
->from('student as tbl1, student_acedemic_process as tbl2')
->where('tbl1.student_id = tbl2.student_id')
->where('tbl2.program_id ='.$program_id)
->where('tbl2.batch_id ='.$batch_id)
//if(!empty($section_id)){
->where('tbl2.section_id ='.$section_id) 
//}
->where('tbl2.status = 1')
->get()->result_array();

foreach ($students as $row)
{

if(isset($_POST['status'.$row['student_id']])){
$attendance_status  =   $this->input->post('status_' . $row['student_id']);
}

$this->db->where('student_id' , $row['student_id']);
$this->db->where('date' , $this->input->post('date'));

$this->db->update('attendance' , array('status' => $attendance_status));
}

$this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
redirect(base_url() . 'index.php?admin/student_attendance/'.$date.'/'.$month.'/'.$year.'/'.$program_id.'/'.$batch_id , 'refresh');



Aucun commentaire:

Enregistrer un commentaire