2015-02-09 33 views
0

我想知道如何從我的模型中調用兩個數據。爲了解釋更加進一步,我想這樣的事情發生,這是我的模型:將兩個不同的sem分隔成不同的表格

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

回答

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循環堅持。但是如果「sem」不同,有條件地預先添加/追加<table>標籤。

+0

它分開,但沒有成績,只有第一列在循環 – 2015-02-09 05:57:08

+0

我只是記錄了代碼,沒有測試它。這是獲得你想要的另一種方法的例子。希望你能弄清楚那裏的任何錯誤:)。 – Scopey 2015-02-09 20:34:27

相關問題