John Davidson

php - Check table box based on database value

0 comments
Message:


I have a table where students are rated based on how punctual, neat, attentive, or polite they are. The scale for rating is from 1-5 (SCALE: 5 = EXCELLENT, 4 = VERY GOOD, 3 = GOOD, 2 = PASS, 1 = FAIR).


So, if a student was rated 5 for punctuality, it records the number 5 both in the database and in view.


However, I would prefer a check to be shown instead of the numbers.


checkboxes


Controller


public function TeacherRating()
{
$studentIds = $this->input->post('student_ids');
$session_ids = $this->input->post('session_ids');
$politenesses = $this->input->post('politenesses');
$punctualities = $this->input->post('punctualities');
$conducts = $this->input->post('conducts');
$honesties = $this->input->post('honesties');

$friendlies = $this->input->post('friendlies');
$neatnesses = $this->input->post('neatnesses');
$handwritings = $this->input->post('handwritings');
$gameses = $this->input->post('gameses');

$sportses = $this->input->post('sportses');
$drawings = $this->input->post('drawings');
$craftses = $this->input->post('craftses');
$musicalskillses = $this->input->post('musicalskillses');
$finalResults = [];
for ($i = 0; $i < sizeof($studentIds); $i++) {
$data['student_id'] = $studentIds[$i];
$this->db->query("DELETE FROM `teacher_ratings` WHERE student_id='".$studentIds[$i]."'");
$data['session_id'] = $session_ids[$i];
$data['politeness'] = $politenesses[$i];
$data['punctuality'] = $punctualities[$i];
$data['conduct'] = $conducts[$i];
$data['honesty'] = $honesties[$i];
$data['friendly'] = $friendlies[$i];
$data['neatness'] = $neatnesses[$i];
$data['handwriting'] = $handwritings[$i];
$data['games'] = $gameses[$i];
$data['sports'] = $sportses[$i];
$data['drawingpainting'] = $drawings[$i];
$data['crafts'] = $craftses[$i];
$data['musicalskills'] = $musicalskillses[$i];
$inserted = $this->Comment_model->TeacherRating($data);
array_push($finalResults, $inserted);
}

if ($finalResults > 0) {
$this->session->set_flashdata('success', 'Teacher Rating Added Successfully');
redirect('rate/rate_students');
}
}

This is my rating form


  <input type="number" max="5" min="1" name="politenesses[]" style="width: 100%;text-align: center;" value="<?php echo $student['politeness']; ?>"></td>


">

My view to display


 <tr>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;">PUNCTUALITY</td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"> <?php echo $teacher_rating->punctuality; ?></td>
</tr>


My Answer:
To display a checkbox instead of the number in your view, you can modify the code like this:

html

PUNCTUALITY

$punctuality = $teacher_rating->punctuality;
for ($i = 1; $i <= 5; $i++) {
if ($i <= $punctuality) {
echo '';
} else {
echo '';
}
}
?>




This code will display checkboxes based on the punctuality rating in the database. If the rating is 5, all checkboxes will be checked. If the rating is less than 5, only the corresponding number of checkboxes will be checked. The checkboxes will be disabled so that the user cannot interact with them.

Rate this post

4 of 5 based on 7518 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us