【问题标题】:Display a table in while loop在while循环中显示表格
【发布时间】:2013-03-07 14:11:39
【问题描述】:

我正在尝试在 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 中显示这样的表格?

【问题讨论】:

  • 您的示例与您的描述不符
  • 对不起。我需要打印一个包含 3 行和每行 3 个表格单元格的表格。

标签: php mysql while-loop html-table


【解决方案1】:

当前代码为每个条目创建一个新表。您想要的是将&lt;table&gt;&lt;/table&gt; 块移出循环,然后您需要一个计数器来跟踪您处理了多少个单元格。如果是偶数,请开始一个新的&lt;tr&gt;。否则,结束当前的&lt;/tr&gt;。请务必检查最后是否完成了&lt;/tr&gt;,如果需要,还可以添加一个空的&lt;td&gt;&lt;/td&gt;

【讨论】:

    【解决方案2】:

    试试这个

    $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) )
    

    【讨论】:

    • 如果有奇数条记录怎么办?
    • @castis 这只是打印 html-table 背后的逻辑示例。
    • 你需要在循环内有tr
    • @DreamEater 感谢您的回复。使用您的代码显示一个表格,其中每行有 2 个单元格。我需要连续显示 3 个表格单元格。
    • @TNK 在image 中,每行有 2 个单元格。无论如何,对于每行 3 个单元格,将 if( $i % 2 == 1 ) 更改为 if( $i % 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";
    
    ?>
    

    【讨论】:

    • 只需将 if ($cells === 3 中的 2 更改为 3 即可解决此问题。我更新了我的例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多