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

I have a PHP code that generates a table with mysql query on the page. I would like to display 3 empty rows below the header row at the top of the table. How can I achieve this? is it possible? thanks for your help and time!

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

You can do it with plain HTML or you can do it with PHP loops (better way if you want to change 3 rows for example to 30, it gives you the flexibility). And please, try to indent your code so that it's easy readable and maintainable. Here is the code:

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

As a coding pattern, when something like this comes up, I like to create a small function that generates the table rows from passed in data, like below.

Note that there's a variety of ways to approach this, this is just one way....

// 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>";
}

Above line <?php put

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

thats all.

Better options have already appeared in the answers, but I'll toss this in here as well: you can union three empty rows into your query result set. Adding the rows in your PHP is better, but you can use this concept if you must rely on your query returning blank rows.

Here is an example of how you might build your $sql. Note that in the example here, I've used '' as the default value, but if these Rp# columns are numeric etc, you should default to what is required.

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

Its old-school using tables but I think this would work as well:

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

Taking the concept of the colspan from d g's answer, you can use PHP's count() function to dynamically determine how many columns will be outputted by your $row assoc array.

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

}