我有一个表,我可以选择并回显列的名称 (th) 和字段数据 (td)。但是用户可以添加和删除列。我如何编写可以适应用户更改的更灵活的代码?我的意思是能够在不知道所有字段的情况下拥有整个表。
<?php
$sql = "SELECT * from eee";
$result = mysql_query($sql,$con);
$id = mysql_field_name($result, 0);
$a = mysql_field_name($result, 1);
$b = mysql_field_name($result, 2);
$c = mysql_field_name($result, 3);
?>
<tr>
<th><input class="gris" type="text" name="<?php echo $id ?>" value="<?php echo $id ?>"/></th>
<th><input class="gris" type="text" name="<?php echo $a ?>" value="<?php echo $a ?>"/></th>
<th><input class="gris" type="text" name="<?php echo $b ?>" value="<?php echo $b ?>"/></th>
<th><input class="gris" type="text" name="<?php echo $c ?>" value="<?php echo $c ?>"/></th>
</tr>
<?php
$result = mysql_query("SELECT * FROM eee");
while($row = mysql_fetch_array($result)) {
?>
<tr>
<td> <input class="blanc" type="text" name="num" value="<?php echo $row['id']?>"/> </td>
<td><input class="blanc" type="text" name="a" value="<?php echo $row['a']?>"/></td>
<td><input class="blanc" type="text" name="b" value="<?php echo $row['b']?>"/></td>
<td><input class="blanc" type="text" name="c" value="<?php echo $row['c']?>"/></td>
</tr>
<?php } ?>
</table>
最佳答案
您正在尝试做的是为穷人提供食物 ORM .我建议您阅读 INFORMATION_SCHEMA .它是一个 ANSI 标准,可以为您提供有关数据库和表的元信息。您可以从那里即时选择列名称,许多现代 RDMS 都支持它。
另一种选择是调查 Doctrine因为它将为您提供此功能。
关于php - 选择并回显所有表格(无论它是什么),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14058851/