有个时候需要将一些table里面的内容显示在页面上让用户参考
到网上瞄了一下,很多table在写的时候就规定了thead的内容和个数,这就显得十分不方便
于是写了一个不限内容不限column个数的表格显示代码
<html>
<head>
<style type="text/css">
thead {}
tbody {}
</style>
</head>
<body>
<script type="text/javascript">
</script>
<?php
$db = mysql_connect("","root","") or die("Could not connect.");
if(!$db)
die("no db");
if(!mysql_select_db("数据库",$db))
die("No database selected.");
$Info = mysql_query("SELECT * FROM 数据表");
while ($property = mysql_fetch_field($Info))
{
$column[]=$property->name;
}
echo "<table border='1'>";
echo "<thead>";
echo "<tr>";
$length = count($column);
for ($i=0; $i < $length; $i++) {
echo "<th>";
echo $column[$i];
echo "</th>";
}
echo "</tr>";
echo "</thead>";
echo "<tbody>";
for ($m=0; $m < 5; $m++) { //这里设置只显示5行
$Info_array=mysql_fetch_array($Info,MYSQL_NUM);
echo "<tr>";
for ($i=0; $i < $length; $i++) {
echo "<td>";
echo $Info_array[$i];
echo "</td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
</body>
</html>
只要将数据库和数据表改成自己想要显示的内容就OK了
这里只显示了前5行的内容,如果想将table全部显示只需要将5改掉就好了