2015-06-13 31 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将再次遍历的第一个记录一个快退/复位法。)

相关问题