php - 基于数据库值的检查表框
问题描述
我有一张表格,根据学生的守时、整洁、专心或礼貌程度对学生进行评分。评分等级为 1-5(等级:5 = 优秀,4 = 非常好,3 = 好,2 = 通过,1 = 一般)。
因此,如果一个学生的守时评分为 5,它会在数据库和视图中记录数字 5。
但是,我更喜欢显示支票而不是数字。
控制器
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');
}
}
这是我的评分表
<input type="number" max="5" min="1" name="politenesses[]" style="width: 100%;text-align: center;" value="<?php echo $student['politeness']; ?>"></td>
">
我要显示的视图
<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>
解决方案
我将根据示例数据制作一个非常简单的版本,您可以希望将其集成到您的实际代码中。这是一项非常常见的编程任务,因此它是一个很好的学习和学习的任务。PHP 和 HTML 的混合有时会变得很难看,所以为了清楚起见,我个人更喜欢使用 PHP 的模板语法。如果你不喜欢它,你也可以切换到echo
或使用普通的{}
PHP 语法。
首先,我将创建一个属性数组和可能的分数。属性是人类可读标签和数组索引键名的映射。在您的情况下,后者也可以是数据库列。
$attributes = [
'Punctuality' => 'punctuality',
'Honesty' => 'honesty',
];
$possibleScores = [1, 2, 3, 4, 5];
接下来,我将模仿您的数据库并为单个学生创建一个跨越属性的分数。
$score = [
'punctuality' => 2,
'honesty' => 5,
];
最后,我要渲染。第一个循环只呈现列标题。这组循环一次执行一个属性,然后每次为单元格评分一个,将单元格的当前值与内部循环的当前值进行比较,并在需要时弹出一个复选框。
<thead>
<tr>
<td></td>
<?php foreach ($possibleScores as $possibleScore): ?>
<th scope="col"><?php echo $possibleScore; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ($attributes as $label => $arrayIndex): ?>
<tr>
<th scope="row"><?php echo $label; ?></th>
<?php foreach ($possibleScores as $possibleScore): ?>
<td class="checkbox">
<input
type="checkbox"
value="<?php echo $possibleScore ?>"
<?php if ($possibleScore === $score[$arrayIndex]): ?>
checked
<?php endif; ?>
/>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
上面的代码呈现了一个与下面非常相似的表格
1 | 2 | 3 | 4 | 5 | |
---|---|---|---|---|---|
准时 | ✔</td> | ||||
诚实 | ✔</td> |
上面的代码只是一个例子。如果您不想要真正的表单复选框,您显然可以只使用图像或其他任何东西。如果您将其保留为表单并且您希望它是可编辑的(我在您的帖子中不清楚),那么您希望将复选框变成单选按钮,并且每一行都应该是相同的名称。但那是另一个帖子。
希望这足以让您入门,但是如果我完全走错了方向,请告诉我。
推荐阅读
- php - 使用 Doctrine2 按 Not Null 分组
- gcc - asm 指令“jmpq *0xa48201(%rip)”到底在做什么?
- c - Pthreads而不是共享变量
- spring - Kotlin - 如何将 BodyToMono 与泛型和嵌套类一起使用 WebFlux
- java - Bazel 在resin/raspberry-pi3-debian 上从头开始构建错误
- javascript - IOS页面间传递数据
- python - 尝试在 celery 任务文件中导入模型时尚未加载应用程序
- tensorflow - 关于 tf.app.flags.DEFINE_enum 的使用
- java - JavaFX 从另一个线程更新 AnchorPane
- typescript - 通用类型'AngularFirestoreDocument
' 需要 2 个类型参数