How to loop sql table data inside a html table



PHP Snippet 1:

...
<!--loophere the table contents here as td in a tr-->
<?php foreach($orders as $key=>$order): ?>
<tr>
  <td>
    content 1
  </td>
  <td>
    content 2
  </td>
</tr>
<?php endforeach; ?>
...

PHP Snippet 2:

    <!DOCTYPE html>
    <html>
    <head>
      <title></title>
    </head>
    <body>
    
     <table style='margin:10px auto;  border-collapse: collapse; border: 1px solid black;  padding-left:10px;'>
       <tr>
         <th colspan='4' style='border: 1px solid black; padding-left:10px;  padding-top: 6px; padding-bottom: 6px;  background-color: #00000061; color: White;    font-weight:400;'>
           <h4>Invoice Order No. # <?php echo $invoice ?> </h4>
         </th>    
       </tr>
       <tr>
         <td colspan='4' style='text-align:left; border: 1px solid #ddd; padding: 5px 20px;'>
         <p style='text-align:right;line-height: 0.2; padding:5px 15px;'> <?php echo $name ?></p>
         <p style='text-align:right; padding:5px 15px;'> <?php echo $address ?> </p>
         <p style='text-align:right;line-height: 0.2; padding:5px 15px;'> <?php echo $phone ?> </p>
         <p style='text-align:right;line-height: 0.2; padding:5px 15px;'> <?php echo $email ?></p>
         <p style='text-align:left;line-height: 0.2; padding:5px 15px;'>Invoice Date:  <?php echo $date ?></p>
      </td>
     </tr>
   <tr style='padding-top: 6px; padding-bottom: 6px;  background-color: #00000061; color: White; padding-left:10px;   font-weight:400;'>
        <th style='border: 0px solid;padding-left:10px;'>ITEM</th>
        <th style='border: 0px solid;padding-left:10px;'>QUANTITY</th>
        <th style='border: 0px solid;padding-left:10px;'>PRICE</th>
        <th style='border: 0px solid;padding-left:10px;'>TOTAL</th>
    </tr>
    <!--loophere the table contents here as td in a tr-->
    <?php foreach($orders as $key=>$order): ?>
    <tr>
      <td>
        content 1
      </td>
      <td>
        content 2
      </td>
    </tr>
    <?php endforeach; ?>
    <!--loop here-->
       <td colspan='4' style='background-color:#0a0512a8;color:white; text-align: center;padding:10px 25px;'><strong>Total: <?php echo $carttotal ?> </strong></td>
     </tr>
  </table> 
</body>
</html>

PHP Snippet 3:

<?php
    include "db.php";
    
    if (isset($_POST['invoicebtn'])) {
    
    
        $invoice = $_POST['invoice'];
    
    
        $result1 = mysqli_query($con, "SELECT * FROM users WHERE invoice='$invoice' ");
        $rowinvoice = $result1->fetch_assoc();
    
        $name = $rowinvoice['name'];
        $email = $rowinvoice['email'];
        $phone = $rowinvoice['phone'];
        $address = $rowinvoice['address'];
        $carttotal = $rowinvoice['orderTotal'];
    
        date_default_timezone_set('Africa/Nairobi');
        $date = date('d-m-y h:i:s');
    
    
    
        $sql = mysqli_query($con, "SELECT * FROM orders WHERE invoice='$invoice' ");
        $emailbody = "<table style='margin:10px auto;  border-collapse: collapse; border: 1px solid black;  padding-left:10px;'>
        <tr>
        <th colspan='4' style='border: 1px solid black; padding-left:10px;  padding-top: 6px; padding-bottom: 6px;  background-color: #00000061; color: White;    font-weight:400;'>
        
        
        <h4>Invoice Order No. # $invoice </h4>
       </th>    
      </tr>
      <tr>
        <td colspan='4' style='text-align:left; border: 1px solid #ddd; padding: 5px 20px;'>
          <p  style='text-align:right;line-height: 0.2; padding:5px 15px;'> $name </p>
          <p style='text-align:right; padding:5px 15px;'> $address </p>
          <p style='text-align:right;line-height: 0.2; padding:5px 15px;'> $phone </p>
          <p style='text-align:right;line-height: 0.2; padding:5px 15px;'> $email</p>
          <p style='text-align:left;line-height: 0.2; padding:5px 15px;'>Invoice Date:  $date </p>
        </td>    
       </tr>
       <tr style='padding-top: 6px; padding-bottom: 6px;  background-color: #00000061; color: White; padding-left:10px;   font-weight:400;'>
            <th style='border: 0px solid;padding-left:10px;'>ITEM</th>
          
            <th style='border: 0px solid;padding-left:10px;'>QUANTITY</th>        
            <th style='border: 0px solid;padding-left:10px;'>PRICE</th>
            <th style='border: 0px solid;padding-left:10px;'>TOTAL<ksh></th>
        </tr>";
        while ($row = mysqli_fetch_assoc($sql)) {
    
    
            /*Data corespond to table columns as bolow*/
            $emailbody .= "
            <tr>
                <td>" . $row['item'] . "</td>
                <td>" . $row['quantity'] . "</td>
                <td>" . $row['price'] . "</td>
                <td>" . $row['quantity'] * $row['price'] . "</td>
            </tr>
            ";
    
            /*I would like to loop all the data from the page and use it in the table below which I will be sending out in an email body*/
        }
    
    
    
    
    
    
    
        $emailbody .= "
      
      <!--loophere the table contents here as td in a tr-->
    
          
    
      <!--loop here-->
         <td colspan='4' style='background-color:#0a0512a8;color:white; text-align: center;padding:10px 25px;'><strong>Total: $  $carttotal </strong></td>
       </tr>
       </table>";
    } else {
        header("Location:orders.php?Anauthorized=true");
    };
    
    ?>
    
    
    <!DOCTYPE html>
    <html>
    
    <head>
        <title></title>
    </head>
    
    <body>
    
        <!--im echooing the variable emailbody to see what will be sent-->
        <?php echo $emailbody; ?>
    
    </body>
    
    </html>