我正在建立一个天气网站,并在一个自定义的预报页面工作。我有一个数据库设置在服务器上的值。下面是我的代码:
$sql = "SELECT * FROM forecast ORDER BY FID";
$result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$i = 0;
echo '<table width="1400" border="1">';
echo '<td>';
while ($forecast = mysql_fetch_array($result)) {
echo '<table width="200" border="1">';
echo '<tr>';
echo "<th colspan='4'>{$forecast['DAY']}</th>";
echo '</tr>';
echo '<tr>';
echo "<td height='100' width='100' colspan='2'>ICON</td>";
echo "<td height='100' width='100' colspan='2'>ROSE</td>";
echo '</tr>';
echo '<tr>';
echo "<td>{$forecast['HIGH']}</td>";
echo "<td>{$forecast['LOW']}</td>";
echo "<td>{$forecast['WIND']}</td>";
echo "<td>{$forecast['GUST']}</td>";
echo '</tr>';
echo '<tr>';
echo "<td>Precip:{$forecast['RAIN']}</td>";
echo "<td>Severe:{$forecast['SEVERE']} </td>";
echo '</tr>';
echo '</table>';
++$i;
}
echo '</td>';
echo '</table>';
但是当我运行它时,我没有得到我想要的水平布局(循环生成一个显示预测的表,我想要一行7个),但我得到了一个垂直布局。见图:http://www.themacvortex.com/TableFail.png
我如何修改我的代码得到水平布局?
您使用单个<td>
创建外部<table>
,周围缺少<tr>
。但真正的问题是:
您的外部<td>
应该读取<tr>
。你的while
应该是这样的:
while ($forecast = mysql_fetch_array($result)) {
echo '<td>'
. '<table width="200" border="1">'
. ..... more ...
. '</table>'
. '</td>
;
}
while
内部在每次while
迭代期间创建新列。
W3C的验证服务可能也有帮助。