2 回答
TA贡献1804条经验 获得超2个赞
你打电话时...
$aocol = count(mysqli_fetch_array($erg, MYSQLI_NUM));
这实际上是在读取第一行,因此它不再可用于以下循环。
我建议重组您的代码,以便在主读取循环内构建头部,但只有当$head它为空时......
//table head
$head = "";
//only if style.css included-->irrelevant
echo '<div class="table"><table id="table">';
//output of table's body --> here must be the bug, I think
while($zeile = mysqli_fetch_array($erg, MYSQLI_NUM)) {
if ( empty ($head) ) {
//counts the amount of columns
$aocol = count($zeile);
for($x = 0; $x < $aocol; $x++) {
//legt alle Informationen des Feldes $x in $finfo, darunter auch den Namen der Spalte.
//puts all information of the field $x in $finfo, also the name of the column
$finfo = mysqli_fetch_field_direct($erg, $x);
//Schreibt die Kopfzeile der Tabelle
//writes the table's head
$head .= "<th>".$finfo->name."</th>";
}
//output of table's head
echo "<th>$head</th>";
}
//new tablerow
echo "<tr>";
//filling the row
foreach($zeile as $feld) {
//format for numbers
$ra = preg_match('/^\d+$/',$feld) ? ' align="right"' : '';
//displaying table data
echo "<td$ra>".$feld."</td>";
}
}
此外,如果您更改MYSQLI_NUM为MYSQLI_ASSOC,那么您可以只使用键名作为列名并删除额外的 API 调用......
while($zeile = mysqli_fetch_array($erg, MYSQLI_ASSOC)) {
if ( empty ($head) ) {
foreach ( $zeile as $name => $value ) {
$head .= "<th>".$name."</th>";
}
//output of table's head
echo "<th>$head</th>";
}
TA贡献1789条经验 获得超10个赞
您应该使用这样的查询来获取所有结果:
$query = "SELECT * FROM table ORDER BY `id` LIMIT 0, 5";
这只会给你前 5 个结果,然后你可以:
$query = "SELECT * FROM table ORDER BY `id` LIMIT 5, 100";
或这个:
$query = "SELECT * FROM table ORDER BY `id` DESC LIMIT 5";
- 2 回答
- 0 关注
- 115 浏览
添加回答
举报