构建 5x6 动态 HTML 表格
我正在尝试使用 PHP 动态构建一个 5x6 的表。现在我的桌子尺寸是 1x30。我可能缺少 if 语句或其他东西,但就是无法弄清楚。一点帮助就太好了。
$data = mysql_query("SELECT * FROM product")
or die(mysql_error());
$info = mysql_fetch_array( $data );
print "<table border cellpadding='0' cellspacing='0'>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr><td>".$info['name'] . " <br /><img src=".$info['pictureURL'] . " /></td></tr>";
}
print "</table>";
这是一个演示 http://nova.it.rit .edu/~mpb8676/FinalProject/compare.php
I'm trying to build a table that is 5x6 dynamically using PHP. Right now I'm getting my table to be 1x30. I'm probably missing an if statement or something but just can't figure it out. A little help would be great.
$data = mysql_query("SELECT * FROM product")
or die(mysql_error());
$info = mysql_fetch_array( $data );
print "<table border cellpadding='0' cellspacing='0'>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr><td>".$info['name'] . " <br /><img src=".$info['pictureURL'] . " /></td></tr>";
}
print "</table>";
Here is a Demo of what it looks like http://nova.it.rit.edu/~mpb8676/FinalProject/compare.php
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用此行为每条记录打印一个新行:
如果您想要一个 5x6 表,那么您必须添加列计数器:
肯定有更有效的方法来解决此问题,但我尝试保留您的代码完整,以便您更好地了解您的问题是什么。
You are printing a new row for each record with this line:
If you want a 5x6 table, then you'll have to add in a column counter:
There are definitely more efficient ways to go about this, but I tried to keep your code in tact so you'd better understand what your issue was.
我相信您需要更多 td:
I believe that you need more td's:
您需要特殊对待
。正确的是,您将为查询中的每个结果输出一个单元格行。相反,您需要:
当您所在的单元格是 6 的偶数倍时,这只会打印
标记,表明您已到达该行末尾。
您将需要一些额外的特殊大小写来处理打印的第一行,因此您的表格开头不会有悬空的
,但这应该足以获得你开始了。您还需要对最后一行进行特殊情况处理,因此即使此时打印的不是“第 6”个单元格,您也可以正确关闭该行。
You need to treat the
<tr>
specially. Right you're outputting a single celled row for every result in your query. Instead, you need:This'll only print the
<tr>
tags when the cell you're on is an even multiple of 6, indicating you've hit the end of the line.You'll need some extra special casing to handle the very first row printed, so you don't have a dangling
</tr>
at the start of your table, but this should be enough to get you started. You'll also need special case handling for the last row, so you properly close the row even if it's not a "6th" cell being printed at that point.