How to create an HTML table in a foreach loop

13,460

Solution 1

Create the table and table header outside the loop.

Populate the table body in the loop, and close the loop.

<table>
    <tr>
        <th>Email</th>
        <th>Name</th>
        <th>Last Name</th>
    </tr>
    <?php
    $i = 0;
    foreach ($result as $r) {
        echo "<tr>";
        echo "<td>" . $r['aluno_sobrenome'] . "</td><td>" . strtolower(trim(($r['aluno_nome']))) . "</td><td>" . strtolower(trim($r['aluno_sobrenome'])) . "</td>";
        echo "</tr>";

        $i++;
    }
    ?>
</table>

Solution 2

Loop through the results, each result outputs one row

echo '<table>';
foreach ($results as $r) {
   echo sprintf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>', $r['email'], $r['name'], $r['last_name']);
}
echo '</table>';

Solution 3

Try this:

<?php 
$i = 0;
echo '<table>';
foreach ($result as $r) {
    echo "<tr><td>{$i}</td><td>{$r['aluno_email'}</td><td>{$r['aluno_nome']}</td></tr>";
    $i++;
}
echo '</table>';

Solution 4

I usually start by checking for results, then build the table:

if ($result->num_rows > 0) {
     echo "<table>
           <tr>
               <th>Email</th>
               <th>Name</th>
               <th>LastName</th>
           </tr>";

I run my loop and output as long as I have results:

// output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<tr>
                   <td>" . $row["Email"]. "</td>
                   <td>" . $row["Name"]. "</td>
                   <td>" . $row["LastName"]. "</td>
               </tr>";

     }
     echo "</table>";
Share:
13,460
Admin
Author by

Admin

Updated on June 28, 2022

Comments

  • Admin
    Admin almost 2 years

    I need to create an HTML table consisting of three columns: email, name, last name within my loop.

    Could anyone give a hint how I can start creating?

    $i=0;
    foreach($result as $r) {
      echo "<pre>";
      print_r( $i ." ". $r['aluno_email'] ."". " | ". $r['aluno_nome']
    . " " .
    $r['aluno_sobrenome'] . " | ". strtolower(trim(($r['aluno_nome'])))."_".strtolower(trim($r['aluno_sobrenome'])));
       echo "<pre>";
        $i++;
    }
    
  • Jay Blanchard
    Jay Blanchard over 8 years
    Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO.
  • kero
    kero over 8 years
    echo sprintf() seems a bit too much, printf() should suffice. But +1 anyway for using such a function
  • Jay Blanchard
    Jay Blanchard over 8 years
    Yes, I did. Code only answers are typically considered bad form and I was suggesting that you could make your answer better to the point that folks will actually upvote your answer. It is your choice whether or not to improve it. Happy Holidays! ¯\_(ツ)_/¯
  • Jay Blanchard
    Jay Blanchard over 8 years
    Why should the OP try this? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO.
  • Jay Blanchard
    Jay Blanchard over 8 years
    Yes. What if a total newbie reads your answer? Things might not be as obvious as you think they are.
  • Jay Blanchard
    Jay Blanchard over 8 years
    It is a site for every walk of programmer, from newbie to the very experienced. Be that as it may, all I did was make a suggestion and it is your choice to implement or not.
  • Ian
    Ian over 8 years
    Please, show by example. Edit my answer, I'd like to see how it can better answer the question asked.
  • Jay Blanchard
    Jay Blanchard over 8 years
    Please read this. for some insight as to why code only answers are not considered good answers and how they're typically handled. If you really want me to improve your answer, I will. Thanks for suggesting that.
  • Ian
    Ian over 8 years
    "If it's short, the code may be the best explanation possible." Also, I suggest you look at the other answer to this question, all of which have the exact same answer as me (all posted after me). None of them have longer or more detailed explanations, and 2 of them have been upvoted.