将两个不同的sem分成不同的表

时间:2015-02-09 03:25:27

标签: php codeigniter view

我想知道如何从我的模型中调用两个数据。为了进一步解释,我希望这种情况发生,这是我的模型:

function result_getGrades($studentid)
        {

            $sql=   "   
                    SELECT g.studentid, sb.subjectcode, s.description, g.final,sb.sem,sb.sy
                    FROM grades g 

                    JOIN subjectblocking sb ON g.blockcode=sb.blockcode

                    JOIN subjects s ON sb.subjectcode=s.subjectcode

                    WHERE g.studentid='$studentid'
                    ORDER BY sb.sem ASC, sb.sy ASC;


                    ";
            $result = $this->db->query($sql);
            $result = $result->result();
            return $result;
        }

这是观点:

<div class="col-md-10">
    <div class="well">
            <table class="table">

                <thead>
                    <th>Subject Code</th>
                    <th>Description</th>
                    <th>Sem</th>
                    <th>School Year</th>
                    <th>Grade</th>
                </thead>
        <?php foreach ($query as $row){ ?>
                    <tr>
                        <td><?php echo $row->subjectcode;?><br></td>
                        <td><?php echo $row->description;?><br></td>
                        <td><?php echo $row->sem;?><br></td>
                        <td><?php echo $row->sy;?><br></td>
                        <td><?php echo $row->final;?><br></td>
                    </tr>

        <?php } ?>  
            </table>

这是它的样子,因为你可以看到它在一个表中,我想分开具有不同sem的表,我该怎么做? enter image description here

1 个答案:

答案 0 :(得分:0)

您可能不得不偏离传统的foreach循环并执行一些while循环,如下所示(以下代码未经测试):

<?php 
while($row = current($query)) {
    // Echo the table HTML
    ?>
<table class="table">
    <thead>
        <th>Subject Code</th>
        <th>Description</th>
        <th>Sem</th>
        <th>School Year</th>
        <th>Grade</th>
    </thead>
    <tbody>
    <?php
    // Loop for all the same "sem"s
    do {
        // Echo the HTML
        ?>
        <tr>
            <td><?php echo $row->subjectcode;?><br></td>
            <td><?php echo $row->description;?><br></td>
            <td><?php echo $row->sem;?><br></td>
            <td><?php echo $row->sy;?><br></td>
            <td><?php echo $row->final;?><br></td>
        </tr>
        <?php
        // Set the pointer the the next value in the array and fetch the row
        $nextRow = next($query);
        // If the next row exists, get the "sem" that's next
        if($nextRow) {
            $nextSem = $nextRow->sem;
        }
        else {
            $nextSem = null;
        }
    }
    // Loop while the upcoming "sem" is the same as the one we just printed
    while($nextSem === $row->sem);
    // Echo the end of the table body
    ?>
    </tbody>
</table>
    <?php
}

另一个选择是坚持使用foreach循环。但如果&#34; sem&#34;有条件地在<table>标签之前添加/附加是不同的。