简体   繁体   English

基于数据库值的检查表框

[英]Check table box based on database value

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).评分等级为 1-5(等级:5 = 优秀,4 = 非常好,3 = 好,2 = 通过,1 = 一般)。

So, if a student was rated 5 for punctuality, it records the number 5 both in the database and in view.因此,如果一个学生的守时评分为 5,它会在数据库和视图中记录数字 5。

However, I would prefer a check to be shown instead of the numbers.但是,我更喜欢显示支票而不是数字。

checkboxes复选框

Controller 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>

I'm just going to make a very simple version of this based on sample data that you can hopefully integrate into your actual code.我将根据示例数据制作一个非常简单的版本,您可以希望将其集成到您的实际代码中。 This is a very common programming task so it is a good one to learn and get down.这是一项非常常见的编程任务,因此它是一个很好的学习和学习的任务。 The mixture of PHP and HTML can get ugly at times, so for the sake of clarity I personally prefer to use PHP's templating syntax. PHP 和 HTML 的混合有时会变得很难看,所以为了清楚起见,我个人更喜欢使用 PHP 的模板语法。 If you don't like it, you could also switch to echo or to use normal {} PHP syntax.如果你不喜欢它,你也可以切换到echo或使用普通的{} PHP 语法。

To start with, I'm going to create an array of attributes and possible scores.首先,我将创建一个属性数组和可能的分数。 The attributes are a map of a human-readable label and an array index key name.属性是人类可读的 label 的 map 和数组索引键名称。 The latter in your case could also be a database column instead.在您的情况下,后者也可以是数据库列。

$attributes = [
    'Punctuality' => 'punctuality',
    'Honesty' => 'honesty',
];

$possibleScores = [1, 2, 3, 4, 5];

Next, I'm going to mimic your database and create a score for a single student across to attributes.接下来,我将模仿您的数据库并为单个学生创建一个跨越属性的分数。

$score = [
    'punctuality' => 2,
    'honesty' => 5,
];

Lastly, I'm going to render.最后,我要渲染。 The first loop just renders the column headers.第一个循环只呈现列标题。 The set of loops does each attribute one at a time, and then each score one at a time for the cells, comparing the cell's current value with the inner loop's current value and kicking out a checkbox when needed.这组循环一次执行一个属性,然后每次为单元格评分一个,将单元格的当前值与内部循环的当前值进行比较,并在需要时踢出一个复选框。

    <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>

The code above renders a table very similar to the below上面的代码呈现了一个与下面非常相似的表格

1 1 2 2 3 3 4 4 5 5
Punctuality准时
Honesty诚实

The above code is just an example.上面的代码只是一个例子。 If you don't want true form checkboxes you can obviously just use images or whatever.如果您不想要真正的表单复选框,您显然可以只使用图像或其他任何东西。 If you keep this as a form and you want it to be editable (which I'm not clear on from your post), then you'd want to turn the checkboxes into radio buttons, and each row should be the same name.如果您将此保留为表单并且希望它是可编辑的(我在您的帖子中不清楚),那么您希望将复选框变成单选按钮,并且每一行都应该是相同的名称。 But that's another post.但那是另一个帖子。

Hopefully this is enough to get you started, but if I'm in the wrong direction completely just let me know.希望这足以让您入门,但是如果我完全走错了方向,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM