如何在codeigniter视图中使用单独的表显示数据

时间:2015-03-09 17:41:38

标签: php sql codeigniter views

我仍然是CodeIgniter的新手,我想在不同的表格中按学期和学年分开成绩。我怎么能这样做?

这是我的模特:

public function result_getGrades($studentid)
    {
            $sql="  SELECT g.studentid, sb.subjectcode, s.description, si.firstname,sb.module, sb.sy, sb.sem, g.final 
                    FROM grades g 

                    JOIN subjectblocking sb ON g.blockcode=sb.blockcode

                    JOIN subjects s ON sb.subjectcode=s.subjectcode

                    JOIN studentinfo si ON g.studentid=si.studentid
                    WHERE g.studentid='$studentid'
                    ORDER BY sb.sem DESC, sb.sy DESC;
                    ";
            $result = $this->db->query($sql);
            $result = $result->result();
            return $result;   
    }

这是我的观点:

<table class="table table-bordered">

                    <tr>
                       <th>SY</th>
                       <th>SEM</th>
                       <th>SUBJCODE</th>
                       <th>SUBJECT DESCRIPTION</th>
                       <th>GRADE</th>
                    </tr>

                <?php foreach ($result as $row){ ?>
                    <tr>
                        <td><?php echo $row->sy;?></td>
                        <td><?php echo $row->sem;?></td>
                        <td><?php echo $row->subjectcode;?></td>
                        <td><?php echo $row->description;?></td>
                        <td><?php echo $row->final;?></td>
                    </tr>           
                <?php } ?>

我希望有人可以帮我解决这个问题。我非常需要一个解决方案。提前谢谢!

顺便说一下,这是我的控制器:

public function getGrades() 
{
        $studentid = $this->session->userdata('studentid');
        $data['studentid'] = $studentid;
        $this->load->view('layout/navbar');

        $this->load->model('Umodel');
        $data['result'] = $this->Umodel->result_getGrades($studentid); 
        $this->load->view('pages/grades', $data);
        $this->load->view('layout/footer_student');
}

这就是表格的样子: http://imgur.com/BAP4bI5

2 个答案:

答案 0 :(得分:0)

您可以创建视图,而不是拆分数据。

    CREATE VIEW 2015_Fall AS
    SELECT g.studentid, sb.subjectcode, s.description,
           si.firstname,sb.module, sb.sy, sb.sem, g.final
    JOIN subjectblocking sb ON g.blockcode=sb.blockcode
    JOIN subjects s ON sb.subjectcode=s.subjectcode
    JOIN studentinfo si ON g.studentid=si.studentid
    WHERE g.studentid='$studentid'
    AND  sb.sem = 'Fall' AND sb.sy='2015'
    ORDER BY g.final DESC;

这将为Fall创建一个视图,使您可以像查看单独的表一样查看数据。但是,您的数据仍然位于同一个表中。您只需拥有单独的数据视图。您可能希望为每个学期/年创建其中一个。您可以对视图进行排序和排序,就像它是普通表一样。

答案 1 :(得分:0)

我会重新排列你的结果,在你的控制器中创建一个很好的数组,然后将该数组发送到你的视图,以便于解析。试试这个:

控制器:

public function getGrades() {
    $studentid = $this->session->userdata('studentid');
    $data['studentid'] = $studentid;
    $this->load->view('layout/navbar');

    $this->load->model('Umodel');
    $result = $this->Umodel->result_getGrades($studentid);
    $final = array();
    $final["semesters"] = array();
    foreach ($result as $res) {
        if (!isset($final["semesters"][$res->sem])) {
            $final["semesters"][$res->sem] = array();
        }
        array_push($final["semesters"][$res->sem], $res);
    }
    $data['result'] = $final;

    $this->load->view('pages/grades', $data);
    $this->load->view('layout/footer_student');
}

查看:

<?php foreach ($result['semesters'] as $sem) { ?>
    <table class = "table table-bordered">
        <tr>
            <th>SY</th>
            <th>SEM</th>
            <th>SUBJCODE</th>
            <th>SUBJECT DESCRIPTION</th>
            <th>GRADE</th>
        </tr>

        <?php foreach ($sem as $row) { ?>
            <tr>
                <td><?= $row->sy; ?></td>
                <td><?= $row->sem; ?></td>
                <td><?= $row->subjectcode; ?></td>
                <td><?= $row->description; ?></td>
                <td><?= $row->final; ?></td>
            </tr>           
        <?php } ?>
    </table>

<?php } ?>