[英]PHP echo mysql table column names for html table header
解决
完美的工作! 这是我的最终代码:
<table>
<thead>
<tr>
<?php
$row = mysql_fetch_assoc($result);
foreach ($row as $col => $value) {
echo "<th>";
echo $col;
echo "</th>";
}
?>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php
// Write rows
mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<?php
foreach($row as $key => $value){
echo "<td>";
echo $value;
echo "</td>";
}
?>
<td><button id="edit_project_button(<?php echo $row['ID']; ?>)" class="edit-project-button edit button" onclick="editproject(<?php echo $row['ID']; ?>)">Edit</button></td>
</tr>
<?php } ?>
</tbody>
</table>
我希望适当地使用mysql_fetch函数回显一个HTML表。 我打算制作一个包含mysql表列名的thead和一个包含mysql表结果集的tbody 。 SQL查询从表中选择几列,并设置默认限制。
问题:它似乎没有打印第一行表数据,其他一切显示(缺少记录#1)
它会在每个列表中显示with列名称,然后跳过第一个记录并成功回显第二行。 例如:
| id | firstname | lastname | date_start | date_end | clientid | members | edit |
| 2 | Cal | Clark | 2012-12-12 | 2012-12-12 | 22 | Rob | (edit button) |
| 3 | Rob | Robin | 2012-12-12 | 2012-12-12 | 33 | Cal | (edit button) |
我100%确定第一条记录将在我的phpmyadmin查询中显示。
这是我的代码:
<table>
<thead>
<tr>
<?php
$row = mysql_fetch_assoc($result);
foreach ($row as $col => $value) {
echo "<th>";
echo $col;
echo "</th>";
}
?>
<th>Edit</th>
</tr>
</thead>
<?php
// Write rows
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row[0]; ?></td>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td><?php echo $row[4]; ?></td>
<td><?php echo $row[5]; ?></td>
<td><?php echo $row[6]; ?></td>
<td><button id="edit_project_button(<?php echo $row[0]; ?>)" class="edit-project-button edit button" onclick="editproject(<?php echo $row[0]; ?>)">Edit</button></td>
</tr>
<?php } ?>
</table>
我现在感到如此无视= /
首先回放你的数据!!
mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result)) {
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.