0

我正在尝试在 while 循环中显示一个表格,其中每行有 3 行和 3 td。但我很困惑如何归档它。

到目前为止,这是我的代码:

while($row = mysqli_fetch_array($result)){

    $testimonial    = $row['testimonial'];
  $cityName         = $row['city_name'];
  $name             = $row['name'];
  $imageName        = $row['image_name'];
  $member           = $row['membership_type'];
  $testimonial  = nl2br($testimonial);

    //Create new testimonial output
    $output  = "<table>\n";
    $output .= "  <tr>\n";
    $output .= "     <td>\n";
    $output .= "      <p>\n";
    $output .= "        <img src=\"".UPLOAD_DIR.$imageName."\" />";
    $output .= "         {$testimonial}";
    $output .= "      </p>\n";
    $output .= "     <p class=\"name\">{$name}<br />\n";
    $output .= "      <span>A Teacher - From {$cityName}</span></p>\n";
    $output .= "   </td>\n";
    $output .= "  </tr>\n";
    $output .= "</table>\n";

    echo $output;
}

上面的代码给了我一个多行表,每行 1 个 td。但这不是我预期的结果。

谁能告诉我如何在我的表格中显示这样的表格While loop

4

3 回答 3

2

当前代码为每个条目创建一个新表。您想要的是将<table></table>部分移出循环,然后您需要一个计数器来跟踪您处理了多少个单元格。如果是偶数,则开始一个新的<tr>. 否则,结束当前</tr>. 确保在最后检查以查看是否已完成 a </tr>,并在需要时可选地添加一个空白<td></td>

于 2013-03-19T02:18:47.787 回答
0

尝试这个

$i = 0;
echo "<table>\n<tr>";
while($row = mysqli_fetch_array($result)){
  $testimonial    = $row['testimonial'];
  $cityName         = $row['city_name'];
  $name             = $row['name'];
  $imageName        = $row['image_name'];
  $member           = $row['membership_type'];
  $testimonial  = nl2br($testimonial);
  //Create new testimonial output
  $output .= "     <td>\n";
  $output .= "      <p>\n";
  $output .= "        <img src=\"".UPLOAD_DIR.$imageName."\" />";
  $output .= "         {$testimonial}";
  $output .= "      </p>\n";
  $output .= "     <p class=\"name\">{$name}<br />\n";
  $output .= "      <span>A Teacher - From {$cityName}</span></p>\n";
  $output .= "   </td>\n";
  echo $output;
  if( $i %2 == 1 )
    echo "</tr><tr>\n";
  $i++;
}
echo "</tr>\n</table>\n";

编辑

一般来说,为了n每行显示单元格,将if语句更改为

if( $i % n == (n - 1) )
于 2013-03-19T02:19:48.600 回答
0

这件事要做

<?php

echo "<table>\n";

$cells = $total = 0;
$total_cells = mysqli_num_rows($result);

while($row = mysqli_fetch_array($result))
{
    $testimonial      = nl2br($row['testimonial']);
    $cityName         = $row['city_name'];
    $name             = $row['name'];
    $imageName        = $row['image_name'];
    $member           = $row['membership_type'];

    if ($cells === 0)
        echo "<tr>\n";

    //Create new testimonial output
    $output = "     <td>\n";
    $output .= "      <p>\n";
    $output .= "        <img src=\"".UPLOAD_DIR.$imageName."\" />";
    $output .= "         {$testimonial}";
    $output .= "      </p>\n";
    $output .= "     <p class=\"name\">{$name}<br />\n";
    $output .= "      <span>A Teacher - From {$cityName}</span></p>\n";
    $output .= "   </td>\n";

    echo $output;

    $cells++;
    $total++;       

    if ($cells === 3 || $total === $total_cells)
    {
        echo "</tr>\n";
        $cells = 0;
    }
}

echo "</table>\n";

?>
于 2013-03-19T02:27:26.263 回答