Php 在while循环中显示表格

Php 在while循环中显示表格,php,mysql,while-loop,html-table,Php,Mysql,While Loop,Html Table,我试图在while循环中显示一个表,其中有3行,每行有3个td。但我不知道该怎么做 这是我目前的代码: while($row = mysqli_fetch_array($result)){ $testimonial = $row['testimonial']; $cityName = $row['city_name']; $name = $row['name']; $imageName = $row['image_

我试图在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;
}
while($row=mysqli\u fetch\u数组($result)){
$ESTIONAL=$row['ESTIONAL'];
$cityName=$row['city_name'];
$name=$row['name'];
$imageName=$row['image_name'];
$member=$row['membership_type'];
$ESTIONAL=nl2br($ESTIONAL);
//创建新的推荐输出
$output=“\n”;
$output.=“\n”;
$output.=“\n”;
$output.=“\n”;
$output.=“”;
$output.=“{$commential}”;
$output.=“

\n”; $output.=“

{$name}
\n”; $output.=“教师-来自{$cityName}

\n”; $output.=“\n”; $output.=“\n”; $output.=“\n”; echo$输出; }
上面的代码给了我一个包含多行的表,每行1个td。但这不是我期望的结果


有人能告诉我如何在我的
While循环中显示这样的表吗

当前代码为每个条目创建一个新表。您需要的是将
片段移出循环,然后您需要一个计数器来跟踪您处理了多少个单元格。如果是偶数,则启动一个新的
。否则,请结束当前的
。确保在结尾处检查是否完成了一个
,如果需要,可以选择添加一个空的

试试这个

$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";
这是不可能的

<?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";

?>


您的示例与您的描述不匹配。我需要打印一个包含3行和每行3个表格单元格的表格。如果记录数为奇数怎么办?@castis这只是打印html表格背后逻辑的一个示例。您需要在loop@DreamEater谢谢你的回复。使用代码显示一个表,每行有2个单元格。我需要在一行中显示3个表格单元格。@在中,您每行有2个单元格。无论如何,对于每行3个单元格,将
if($i%2==1)
更改为
if($i%3==2)
。只需将
if($cells==3
中的2更改为3即可解决此问题。我更新了我的示例。
<?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";

?>