我使用这个: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>";
}
}
}
我怎么能输出的所有元素,也是第一个?
最有可能的'$ result-> fetchColumn ()'已经获取了第一条记录,并使其对foreach循环“不可用”。 – VolkerK
你有没有检查是否有五个结果? –
@VigneshBala,是的,我检查过它。我将代码粘贴到phpmyadmin中,并将会话变量更改为1,例如,它工作。 –