2015-06-13 34 views
1

我使用這個:foreach ($result as $row) { $row["testID"], $row["subject"], ... };輸出所有的值。
SQL的工作正常,但當我去頁面它總是顯示我沒有數組中的第一個元素的所有輸出。
例如,如果我有5次測試,以顯示給用戶它僅示出了測試2,3,4和5,但不是第一個陣列$result.
這是所有的代碼(抱歉它在荷蘭)中:
sql正確,但在php它跳過第一個結果

require_once("common.inc.php"); 
    require_once("DataObject.class.php"); 

    class StatistiekAanpassen extends DataObject { 
     public function toetsen() { 
      $conn = parent::connect(); 

      $order = isset($_GET["order"]) ? preg_replace("/[^ a-zA-Z]/", "", $_GET["order"]) : "toetsID"; 

      /*SQL om alle toetsen op te halen*/ 
      $sql = "SELECT toetsID, onderwerp, puntentotaal, vak 
        FROM Toets 
        WHERE NOT EXISTS 
         (SELECT * 
         FROM LeerlingToets 
         WHERE Toets.toetsID = LeerlingToets.toetsID AND 
         LeerlingToets.gebruikerID = " . $_SESSION["member"]->getValue("gebruikerID") . ") order by " . $order; 

      $result = $conn->query($sql); 

      if ($result->fetchColumn()) { 
       echo "<table id=\"toetsTable\">"; 
       ?> 
       <tr> 
        <th id="toetsTableIDth"><?php if ($order != "toetsID") { ?><a href="statistiekenAanpassen.php?order=toetsID"><?php } ?>Nr<?php if ($order != "toetsID") { ?></a><?php } ?></th> 
        <th id="toetsTableIDth"><?php if ($order != "onderwerp") { ?><a href="statistiekenAanpassen.php?order=onderwerp"><?php } ?>Onderwerp<?php if ($order != "onderwerp") { ?></a><?php } ?></th> 
        <th id="toetsTableIDth"><?php if ($order != "vak") { ?><a href="statistiekenAanpassen.php?order=vak"><?php } ?>Vak<?php if ($order != "vak") { ?></a><?php } ?></th> 
        <th id="toetsTableIDth"><?php if ($order != "puntentotaal") { ?><a href="statistiekenAanpassen.php?order=puntentotaal"><?php } ?>Puntentotaal<?php if ($order != "puntentotaal") { ?></a><?php } ?></th> 
       </tr> 
       <?php 
        foreach ($result as $row) { 
         echo "<tr><td class=\"IDtd\"><a href=\"statistiekenAanpassen.php?toetsID=" . $row["toetsID"] . "\">" . $row["toetsID"] . "</a></td> 
        <td><a href=\"statistiekenAanpassen.php?toetsID=" . $row["toetsID"] . "\">" . $row["onderwerp"] . "</a></td> 
        <td><a href=\"statistiekenAanpassen.php?toetsID=" . $row["toetsID"] . "\">" . $row["vak"]. "</a></td> 
        <td><a href=\"statistiekenAanpassen.php?toetsID=" . $row["toetsID"] . "\">" . $row["puntentotaal"]. "</a></td></tr>"; 
        } 
       echo "</table>"; 
      } else { 
       echo "<p>Er zijn nog geen toetsen.<p>"; 
       echo "<p>OF<p>"; 
       echo "<p>U heeft voor alle toetsen al punten ingegeven.<p>"; 
      } 
     } 
    } 


我怎麼能輸出的所有元素,也是第一個?

+2

最有可能的'$ result-> fetchColumn ()'已經獲取了第一條記錄,並使其對foreach循環「不可用」。 – VolkerK

+0

你有沒有檢查是否有五個結果? –

+0

@VigneshBala,是的,我檢查過它。我將代碼粘貼到phpmyadmin中,並將會話變量更改爲1,例如,它工作。 –

回答

2

的fetchcolumn使得第一列不可用,請使用

$result->fetch_assoc() 
+0

fetch_assoc()不適用於我,但我不知道爲什麼 –

+0

然後嘗試使用$ results-> fetch_object();但要顯示這個,你需要用$ row-> toetsID引用你的列; –

+0

我需要參考的地方以及如何。對不起,我不熟悉這一點。 –

3

最有可能的$ result-> fetchColumn()已經取得的第一個記錄,並使其「不可用」你的foreach循環。
你可以使用一個標誌變量來「檢查」這個迭代是否是第一個,如果是的話,打印表頭。並且在循環之後通過該標誌檢查是否至少打印了一條記錄並且如果不打印錯誤消息。

或者,您可以從for(each)循環切換到do-while循環,並讓循環體打印「previous」記錄的內容,即已經在if條件內提取的記錄或「在」之前迭代的while-condition中。

if (!($row=$result->fetchRow())) { // or whatever the method to fetch one record is called 
    echo " 
     <p>Er zijn nog geen toetsen.<p> 
     <p>OF<p> 
     <p>U heeft voor alle toetsen al punten ingegeven.<p> 
    "; 
} 
else { 
    // <-- put table tag and table header here --> 

    do { 
     // <-- print the current table row here --> 
    } 
    while( $row=$result->fetchRow()); // again: or whatever the method to fetch one record is called 

    // <-- close the table tag here --> 
} 

(也有可能是將記錄指針回到開始之前,這樣的foreach將再次遍歷的第一個記錄一個快退/復位法。)

相關問題