显示学生的每月出勤报告

时间:2017-03-10 12:38:04

标签: php codeigniter

为什么不告诉我谁在场,谁不在场?

该文件为here

表格是出席栏目:

  

attendance_id |时间戳|一年| class_id | section_id |学生卡   | class_routine_id |状态

状态值:(0未定义,1存在,2不存在)

<?php
                        $data = array();

                        $students = $this->db->get_where('enroll', array('class_id' => $class_id, 'year' => $running_year, 'section_id' => $section_id))->result_array();

                        foreach ($students as $row):
                            ?>
                    <tr>
                        <td style="text-align: center;">
                        <?php echo $this->db->get_where('student', array('student_id' => $row['student_id']))->row()->name; ?>
                        </td>
                        <?php
                        $status = 0;
                        for ($i = 1; $i <= $days; $i++) {
                            $timestamp = strtotime($i . '-' . $month . '-' . $year[0]);
                            $this->db->group_by('timestamp');
                            $attendance = $this->db->get_where('attendance', array('section_id' => $section_id, 'class_id' => $class_id, 'year' => $running_year, 'timestamp' => $timestamp, 'student_id' => $row['student_id']))->result_array();


                            foreach ($attendance as $row1):
                                $month_dummy = date('d', $row1['timestamp']);

                                if ($i == $month_dummy)
                                $status = $row1['status'];

                                 endforeach;
                            ?>

                            <td style="text-align: center;">
        <?php if ($status ==1 ) { ?>
                                    <i class="entypo-record" style="color: #00a651;"></i>
                        <?php  } if($status == 2)  { ?>
                                    <i class="entypo-record" style="color: #ee4749;"></i>
        <?php  } $status =0;?>


                            </td>

1 个答案:

答案 0 :(得分:0)

我想我发现了你的问题。你错过了每个周期的结束和其他几个括号。我不知道我是否以一种好的方式将你的代码放在一起,但至少它现在应该写得更好。我把两个相同的代码放在这里用“不同的语法”你可以看看看起来更好。

<?php
$data = array();

$students = $this->db->get_where('enroll', array('class_id' => $class_id, 'year' => $running_year, 'section_id' => $section_id))->result_array();

foreach ($students as $row):
    ?>
    <tr>
        <td style="text-align: center;">
            <?php echo $this->db->get_where('student', array('student_id' => $row['student_id']))->row()->name; ?>
        </td>
        <?php
        $status = 0;
        for ($i = 1; $i <= $days; $i++) {
            $timestamp = strtotime($i . '-' . $month . '-' . $year[0]);
            $this->db->group_by('timestamp');
            $attendance = $this->db->get_where('attendance', array('section_id' => $section_id, 'class_id' => $class_id, 'year' => $running_year, 'timestamp' => $timestamp, 'student_id' => $row['student_id']))->result_array();
        }
        foreach ($attendance as $row1):
            $month_dummy = date('d', $row1['timestamp']);

            if ($i == $month_dummy) {
                $status = $row1['status'];
            }
        endforeach;
    endforeach;
    ?>

    <td style="text-align: center;">
        <?php if ($status == 1) { ?>
            <i class="entypo-record" style="color: #00a651;"></i>
        <?php } if ($status == 2) { ?>
            <i class="entypo-record" style="color: #ee4749;"></i>
        <?php } $status = 0; ?>
    </td>

而且我建议你使用这种语法(我认为最好回应html标签然后使用?php十次,但它的每个人选择):

<?php

$data = array();

$students = $this->db->get_where('enroll', array('class_id' => $class_id, 'year' => $running_year, 'section_id' => $section_id))->result_array();

foreach ($students as $row):

    echo '<tr>
        <td style="text-align: center;">' .
    $this->db->get_where('student', array('student_id' => $row['student_id']))->row()->name .
    '</td>';
    $status = 0;
    for ($i = 1; $i <= $days; $i++) {
        $timestamp = strtotime($i . '-' . $month . '-' . $year[0]);
        $this->db->group_by('timestamp');
        $attendance = $this->db->get_where('attendance', array('section_id' => $section_id, 'class_id' => $class_id, 'year' => $running_year, 'timestamp' => $timestamp, 'student_id' => $row['student_id']))->result_array();
    }
    foreach ($attendance as $row1):
        $month_dummy = date('d', $row1['timestamp']);

        if ($i == $month_dummy) {
            $status = $row1['status'];
        }
    endforeach;
endforeach;

echo '<td style="text-align: center;">';
if ($status === 1) {
    echo '<i class="entypo-record" style="color: #00a651;"></i>';
} else if ($status == 2) {
    echo '<i class="entypo-record" style="color: #ee4749;"></i>';
}
$status = 0;
echo '</td>';