0

I am new to HTML, I am trying to send emails in PHP with a table attachment, the best idea I could come up with was this:

$table = "<html>
              <head>
                    <style>
                    table {
                        font-family: arial, sans-serif;
                        border-collapse: collapse;
                        width: 100%;
                    }

                    td, th {
                        border: 1px solid #dddddd;
                        text-align: left;
                        padding: 8px;
                    }

                    tr:nth-child(even) {
                        background-color: #dddddd;
                    }
                    </style>
                    </head>
                    <body>
                    <table>
                    <tr>
                      <th>Account</th>
                      <th>Credit used</th>
                      <th>Sent messages</th>
                      <th>Balance</th>
                    </tr>
                      <?php foreach ($statistics as $row) {
                    <tr>
                      <td>". $row["username"] ."</td>
                      <td>". $row["creditUsed"] ."</td>
                      <td>". $row["sentMessages"] ."</td>
                      <td>". $row["balance"] ."</td>
                    </tr>
                     }
                     ?>
                    </body>
                    </html>
                  </table>";  

This worked fine until I added the loop, it is treated like a string and gives the error Notice: Array to string conversion I do not know how to make it work

2 Answers 2

1

Usage of Quotes is wrong in your code.

Try to make HTML structure in proper manner like this:-

<html>
    <head>
        <style>
            table {
                font-family: arial, sans-serif;
                border-collapse: collapse;
                width: 100%;
            }

            td, th {
                border: 1px solid #dddddd;
                text-align: left;
                padding: 8px;
            }

            tr:nth-child(even) {
                background-color: #dddddd;
            }
        </style>
    </head>
    <body>
        <table>
        <tr>
          <th>Account</th>
          <th>Credit used</th>
          <th>Sent messages</th>
          <th>Balance</th>
        </tr>

          <?php foreach ($statistics as $row) {?>
                    <tr>
                      <td><?php echo $row["username"];?></td>
                      <td><?php echo $row["creditUsed"];?></td>
                      <td><?php echo $row["sentMessages"];?></td>
                      <td><?php echo $row["balance"];?></td>
                    </tr>
         }
         ?>
    </body>
</html>
</table>

Note:-

Make sure page extension must be .php not .html

Make sure $statistics is set+not-empty+available on this page. (have some values too)

Since you want it to use in PhpMailer so do like below:-

$table = 
"<html>
    <head>
        <style>
            table {
                font-family: arial, sans-serif;
                border-collapse: collapse;
                width: 100%;
            }

            td, th {
                border: 1px solid #dddddd;
                text-align: left;
                padding: 8px;
            }

            tr:nth-child(even) {
                background-color: #dddddd;
            }
        </style>
    </head>
    <body>
        <table>
            <tr>
              <th>Account</th>
              <th>Credit used</th>
              <th>Sent messages</th>
              <th>Balance</th>
            </tr>";

            <?php foreach ($statistics as $row) {
                  $table .= "<tr><td>". $row["username"] ."</td><td>". $row["creditUsed"] ."</td><td>". $row["sentMessages"] ."</td><td>". $row["balance"] ."</td></tr>";
            }
            ?>
$table .= "</body></html></table>";  
Sign up to request clarification or add additional context in comments.

2 Comments

I have edited your answer a bit, it works now, thanks.
@Randoroxford glad to help you :):)
1

It is better to use following structure with HTML:

<?php foreach ($statistics as $row): ?> 
   <tr>
     <td><?= $row["username"]; ?></td>
     <td><?= $row["creditUsed"]; ?></td>
     <td><?= $row["sentMessages"]; ?></td>
     <td><?= $row["balance"]; ?></td>
   </tr>
<?php endforeach; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.