【发布时间】:2015-06-13 09:05:42
【问题描述】:
我使用这个:foreach ($result as $row) { $row["testID"], $row["subject"], ... }; 输出所有值。
sql 工作正常,但是当我转到页面时,它总是向我显示所有输出,而没有数组中的第一个元素。
例如,如果我有 5 个测试要显示给用户,它只显示测试 2、3、4 和 5,而不是数组 $result. 中的第一个测试
这是所有代码(抱歉是荷兰语):
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()already 很可能已经获取了第一条记录,并使其对您的 foreach 循环“不可用”。 -
你检查是否有五个结果来了?
-
@VigneshBala,是的,我已经检查过了。例如,我将代码粘贴到 phpmyadmin 中并将会话变量更改为 1,它就可以工作了。
-
@VolkerK 我该如何解决这个问题?