I have a web app that I had someone do a bit of customization on. It was to create bulk import of scores of students.
As at the time, the students only have exam scores (i.e just one column for scores).
std_id | scores
---------------
101 | 80
However, the students now have a myriad of test scores and an exam scores.
std_id | ca1 | ca2 |ca3 | ca4 | exam
------------------------------------
101 | 7 | 8 | 4 | 8 | 45
Well, I figured it should be easy enough to look through his code and see how I could work on it to allow for bulk import of the new tests columns.
However, I got stuck trying to figure how the value for marks
is gotten.
Here is the original code:
Controller:
public function uploadfile()
{
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('file', $this->lang->line('image'), 'callback_handle_upload');
if ($this->form_validation->run() == false) {
$data = array(
'file' => form_error('file'),
);
$array = array('status' => 0, 'error' => $data);
echo json_encode($array);
} else {
$return_array = array();
//====================
if (isset($_FILES["file"]) && !empty($_FILES['file']['name'])) {
$fileName = $_FILES["file"]["tmp_name"];
if (isset($_FILES["file"]) && !empty($_FILES['file']['name']) && $_FILES["file"]["size"] > 0) {
$file = fopen($fileName, "r");
$flag = true;
while (($column = fgetcsv($file, 10000, ",")) !== false) {
if ($flag) {
$flag = false;
continue;
}
if (trim($column['0']) != "" && trim($column['1']) != "" && trim($column['2']) != "") {
$return_array[] = json_encode(
array(
'adm_no' => $column['0'],
'attendence' => $column['1'],
'marks' => number_format($column['2'], 2, '.', ''), <!-- Where is this gotten from?-->
'note' => $this->encoding_lib->toUTF8($column['3']),
)
);
}
}
}
$array = array('status' => '1', 'error' => '', 'student_marks' => $return_array);
echo json_encode($array);
}
//=============
}
}
View
<tbody>
<?php
if (empty($resultlist)) {
?>
<tr>
<td colspan="7" class="text-danger text-center"><?php echo $this->lang->line('no_record_found'); ?></td>
</tr>
<?php
} else {
foreach ($resultlist as $student) {
?>
<tr class="std_adm_<?php echo $student['admission_no']; ?>">
<td><?php echo $student['admission_no']; ?></td>
<td style="white-space: nowrap;"><?php echo $student['firstname'] . " " . $student['lastname']; ?></td>
<td>
<div>
<?php
foreach ($attendence_exam as $attendence_key => $attendence_value) {
$chk = ($student['exam_group_exam_result_attendance'] == $attendence_value) ? "checked='checked'" : "";
?>
<label class="checkbox-inline"><input type="checkbox" class="attendance_chk" name="exam_group_student_attendance_<?php echo $student['exam_group_class_batch_exam_students_id']; ?>" value="<?php echo $attendence_value; ?>" <?php echo $chk; ?>><?php echo $this->lang->line($attendence_value); ?></label>
<?php
}
?>
</div>
</td>
<td> <input type="number" class="marksssss form-control" name="exam_group_student_mark_<?php echo $student['exam_group_class_batch_exam_students_id']; ?>" value="<?php echo $student['exam_group_exam_result_get_marks']; ?>" step="any"></td>
<td> <input type="text" class="form-control note" name="exam_group_student_note_<?php echo $student['exam_group_class_batch_exam_students_id']; ?>" value="<?php echo $student['exam_group_exam_result_note']; ?>"></td>
</tr>
<?php
}
}
?>
</tbody>
Script:
<script type="text/javascript">
$(document).ready(function () {
$(document).on('click', "#btnSubmit", function (event) {
//stop submit the form, we will post it manually.
event.preventDefault();
var file_data = $('#my-file-selector').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: baseurl + "/admin/examgroup/uploadfile",
type: 'POST',
dataType: 'JSON',
data: form_data,
contentType: false,
cache: false,
processData:false,
beforeSend: function () {
$('#examfade,#exammodal').css({'display': 'block'});
},
success: function (data) {
$('#fileUploadForm')[0].reset();
if (data.status == "0") {
var message = "";
$.each(data.error, function (index, value) {
message += value;
});
errorMsg(message);
} else {
var arr = [];
$.each(data.student_marks, function (index) {
var s = JSON.parse(data.student_marks[index]);
arr.push({
adm_no: s.adm_no,
attendence: s.attendence,
marks: s.marks,
note: s.note
});
});
//===============
$.each(arr, function (index, value) {
var row=$('.marksEntryForm').find('table tbody').find('tr.std_adm_' + value.adm_no);
row.find("td input.marksssss").val(value.marks);
row.find("td input.note").val(value.note);
if(value.attendence == 1){
row.find("td input.attendance_chk").prop( "checked", true );
}else{
row.find("td input.attendance_chk").prop( "checked", false);
}
});
//=================
}
},
error: function (xhr) { // if error occured
alert("Error occured.please try again");
$('#examfade,#exammodal').css({'display': 'none'});
},
complete: function () {
$('#fileUploadForm')[0].reset();
$('#examfade,#exammodal').css({'display': 'none'});
}
});
});
});
</script>
This is what the bulk import file looks like:
A | B | C | D
--------------------------------------
adm_no | status | marks | note
Now, I want the file to be like this:
A | B | C | D | E | F | G | H |
-----------------------------------------------------------------
adm_no | status | ca1 | ca2 | ca3 | ca4 |exam | note