display monthly attendance report of an student

2019-09-18 15:26发布

问题:

Why is it not showing me who is present and who is not?

The file are here.

The table is attendance with columns:

attendance_id | timestamp | year | class_id | section_id | student_id | class_routine_id | status

Status values: (0 undefined, 1 present, 2 absent)


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

I think I found your problem. You missed the end of a for each cycle and few other brackets. I dont know if I put your code together in a good way but at least it should be written better now. Ive put two same codes here with "different syntax" you can take a look which looks better.

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

And also i suggest you using this syntax (i think its better to echo the html tags then using ?php ten times but its everyones choice):

<?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>';