如何在PHP生成的表的顶部显示空行

时间:2018-05-10 20:18:31

标签: php

我有一个PHP代码,可以在页面上生成一个带有mysql查询的表。我想在表格顶部的标题行下面显示3个空行。我怎样才能做到这一点?可能吗?谢谢你的帮助和时间!

<html>
<head>
</head>
<body>

 <table class="tbrp">
 <tr>
  <th>Rp1</th> 
  <th>Rp2</th>
  <th>Rp3</th>
  <th>Rp4</th>
  <th>Rp5</th>
  <th>Rp6</th>
  <th>Rp7</th>
  <th>Rp8</th>
 </tr>


 <?php

  include ("config.php");

  $sql = "SELECT Rp1, Rp2, Rp3, Rp4, Rp5, Rp6, Rp7, Rp8 FROM Rptable";
  $result = $conn->query($sql);
  if ($result->num_rows > 0) {

   // output data of each row
   while($row = $result->fetch_assoc()) 
   {
        echo "<tr><td>" . $row["Rp1"] . "</td><td>" . $row["Rp2"]. "</td><td>". $row["Rp3"]. "</td><td>". $row["Rp4"].  "</td><td>".$row["Rp5"]. "</td><td>". $row["Rp6"]. "</td><td>". $row["Rp7"] . "</td><td>".$row["Rp8"] .  "</td></tr>";

   }

      echo "</table>";

} else { echo "0 results"; }
$conn->close();

    ?>   

</table>



    </body>
</html>

6 个答案:

答案 0 :(得分:4)

以上<?php

<tr></tr> <tr></tr> <tr></tr>

这就是全部。

答案 1 :(得分:3)

您可以使用纯HTML执行此操作,或者您可以使用PHP循环执行此操作(如果您希望将3行更改为30,则更好的方法,它为您提供了灵活性)。请尽量缩进您的代码,以便它易于阅读和维护。这是代码:

<html>
    <head>
    </head>
    <body>
        <table class="tbrp">
            <tr>
                <th>Rp1</th> 
                <th>Rp2</th>
                <th>Rp3</th>
                <th>Rp4</th>
                <th>Rp5</th>
                <th>Rp6</th>
                <th>Rp7</th>
                <th>Rp8</th>
            </tr>
             <?php
                include ("config.php");

                for ($i = 0; $i < 3; $i++) {
                    echo "<tr>";

                    for ($j = 0; $j < 8; $j++) {
                        echo "<td></td>";
                    }

                    echo "</tr>";
                }

                $sql = "SELECT Rp1, Rp2, Rp3, Rp4, Rp5, Rp6, Rp7, Rp8 FROM Rptable";
                $result = $conn->query($sql);
                if ($result->num_rows > 0) {
                    // output data of each row
                    while ($row = $result->fetch_assoc()) {
                        echo "<tr><td>" . $row["Rp1"] . "</td><td>" . $row["Rp2"]. "</td><td>". $row["Rp3"]. "</td><td>". $row["Rp4"].  "</td><td>".$row["Rp5"]. "</td><td>". $row["Rp6"]. "</td><td>". $row["Rp7"] . "</td><td>".$row["Rp8"] .  "</td></tr>";
                    }

                    echo "</table>";
                } else {
                    echo "0 results";
                }

                $conn->close();
            ?>
        </table>
    </body>
</html>

答案 2 :(得分:3)

作为一种编码模式,当出现这样的情况时,我喜欢创建一个小函数,从传入的数据生成表行,如下所示。

请注意,有多种方法可以解决这个问题,这只是一种方式......

// output empty rows...
$empty_rows = 3;
for ( $i = 0; $i < $empty_rows; $i++ ) {
  output_table_row();
}

// output data of each row
while($row = $result->fetch_assoc()) {
    output_table_row( $row );
}

/** 
 * Output an HTML row based on the passed-in $row variable
 */
function output_table_row( $row = NULL ) {
   if ( empty( $row ) ) {
       $row = [
           'Rp1' => '', 
           'Rp2' => '', 
           'Rp3' => '', 
           'Rp4' => '',
           'Rp5' => '',
           'Rp6' => '',
           'Rp7' => '',
           'Rp8' => '',
   }

   echo "<tr><td>" . $row["Rp1"] . "</td><td>" . $row["Rp2"]. "</td><td>". $row["Rp3"]. "</td><td>". $row["Rp4"].  "</td><td>".$row["Rp5"]. "</td><td>". $row["Rp6"]. "</td><td>". $row["Rp7"] . "</td><td>".$row["Rp8"] .  "</td></tr>";
}

答案 3 :(得分:3)

它的老式学校使用桌子,但我认为这也会有效:

<tr><td colspan=8></td></tr>
<tr><td colspan=8></td></tr>
<tr><td colspan=8></td></tr>

答案 4 :(得分:2)

答案中已经出现了更好的选项,但我也会在这里抛出:你可以在查询结果集中union三个空行。在PHP中添加行更好,但如果必须依赖于返回空白行的查询,则可以使用此概念。

以下是如何构建$sql的示例。请注意,在此示例中,我使用''作为默认值,但如果这些Rp#列是数字等,则应默认为所需内容。

// Blank row 1
$sql = "SELECT '' as Rp1, '' as Rp2, '' as Rp3, '' as Rp4, '' as Rp5, '' as Rp6, '' as Rp7, '' as Rp8 ";
$sql .= "UNION ALL ";
// Blank row 2
$sql = "SELECT '' as Rp1, '' as Rp2, '' as Rp3, '' as Rp4, '' as Rp5, '' as Rp6, '' as Rp7, '' as Rp8 ";
$sql .= "UNION ALL ";
// Blank row 3
$sql = "SELECT '' as Rp1, '' as Rp2, '' as Rp3, '' as Rp4, '' as Rp5, '' as Rp6, '' as Rp7, '' as Rp8 ";
$sql .= "UNION ALL ";
// Results
$sql .= "SELECT Rp1, Rp2, Rp3, Rp4, Rp5, Rp6, Rp7, Rp8 FROM Rptable";

答案 5 :(得分:2)

d g's answer获取colspan的概念,您可以使用PHP的count()函数动态确定$row assoc数组将输出的列数。

// Add a flag for whether the blank rows have been outputted
$output_blanks = true;

// output data of each row
while($row = $result->fetch_assoc()) 
{
    // Output the blank rows, if required.
    if ($output_blanks === true)
    {
        for ($i=0; $i<3; $i++)
        {
            echo '<tr><td colspan="' . count($row) . '"></tr>';
        }
        $output_blanks = false;
    }

    // Output the row result
    echo "<tr><td>" . $row["Rp1"] . "</td><td>" . $row["Rp2"]. "</td><td>". $row["Rp3"]. "</td><td>". $row["Rp4"].  "</td><td>".$row["Rp5"]. "</td><td>". $row["Rp6"]. "</td><td>". $row["Rp7"] . "</td><td>".$row["Rp8"] .  "</td></tr>";

}