我使用這個: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,例如,它工作。 –