How to display empty rows at the top of PHP generated table

  • A+
Category:Languages

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> 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: