构建 5x6 动态 HTML 表格

发布于 2025-01-08 01:37:57 字数 645 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄与心 2025-01-15 01:37:57

您正在使用此行为每条记录打印一个新行:

Print "<tr><td>".$info['name'] . "  <br /><img src=".$info['pictureURL'] . " /></td></tr>";

如果您想要一个 5x6 表,那么您必须添加列计数器:

$data = mysql_query("SELECT * FROM product") 
or die(mysql_error());
$info = mysql_fetch_array( $data );
print "<table border cellpadding='0' cellspacing='0'>";
$numCols = 5;   // Max number of columns
$colCount = 0;  // Current column count (the counter)
while($info = mysql_fetch_array( $data )) 
{ 
    if ($colCount == 0) {
        Print "<tr>";
    }

    Print "<td>".$info['name'] . "  <br /><img src=".$info['pictureURL'] . " /></td>";

    $colCount++;

    if ($colCount >= $numCols) {
        Print "</tr>";
        $colCount = 0;
    }
}

print "</table>";

肯定有更有效的方法来解决此问题,但我尝试保留您的代码完整,以便您更好地了解您的问题是什么。

You are printing a new row for each record with this line:

Print "<tr><td>".$info['name'] . "  <br /><img src=".$info['pictureURL'] . " /></td></tr>";

If you want a 5x6 table, then you'll have to add in a column counter:

$data = mysql_query("SELECT * FROM product") 
or die(mysql_error());
$info = mysql_fetch_array( $data );
print "<table border cellpadding='0' cellspacing='0'>";
$numCols = 5;   // Max number of columns
$colCount = 0;  // Current column count (the counter)
while($info = mysql_fetch_array( $data )) 
{ 
    if ($colCount == 0) {
        Print "<tr>";
    }

    Print "<td>".$info['name'] . "  <br /><img src=".$info['pictureURL'] . " /></td>";

    $colCount++;

    if ($colCount >= $numCols) {
        Print "</tr>";
        $colCount = 0;
    }
}

print "</table>";

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.

紫瑟鸿黎 2025-01-15 01:37:57

我相信您需要更多 td:

  Print "<tr><td>".$info['name'] . "</td><td><img src=".$info['pictureURL'] . " /></td><td>and</td><td>so</td><td>on</td></tr>"; 

I believe that you need more td's:

  Print "<tr><td>".$info['name'] . "</td><td><img src=".$info['pictureURL'] . " /></td><td>and</td><td>so</td><td>on</td></tr>"; 
短暂陪伴 2025-01-15 01:37:57

您需要特殊对待。正确的是,您将为查询中的每个结果输出一个单元格行。相反,您需要:

$cells_printed = 0;
while($info = mysql_fetch_array( $data )) {
   if ($cells_printed % 6 == 0) {
      echo '</tr><tr>'; // start a new row
   }
   echo '<td>blahblahblah</td>';
   $cells_printed++;
}

当您所在的单元格是 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:

$cells_printed = 0;
while($info = mysql_fetch_array( $data )) {
   if ($cells_printed % 6 == 0) {
      echo '</tr><tr>'; // start a new row
   }
   echo '<td>blahblahblah</td>';
   $cells_printed++;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文