1

The code below generates an HTML email upon user submit. The email is sent successfully, but the problem I am having is displaying the contents of an array which will contain 1 or more values.

Initially, the user clicks 1 or more checkboxes to get the container numbers. You will see the variable for containerNumber below, which is in an array. The user also manually enters the trucker email which is also turned into a PHP variable. The username is stored in a session is also converted into another PHP variable.

 <?php
   $containerArray = explode(',', $_POST['containerNumber']);
   $trucker_email = mysql_real_escape_string(stripslashes($_POST['trucker_email']));
   $username = $_SESSION['username'];   

Now I generate the variables for the email:

   $to = $trucker_email;
   $subject = 'Container Numbers';
   $headers = "From: " . $username . "\r\n";
   $headers .= "MIME-Version: 1.0\r\n";
   $message = "You have received a message with Containter Numbers:<br />";
   $message .= "Greetings " . stripslashes($_POST['trucker_name']) . "<br />";

Now here's the part that I am trying to get working. It's the HTML table that should show each container number in it's own table cell:

   $message .= '<html><body>';
   $message .= '<table rules="all" style="border-color: #62c462" cellpadding="10">';
   $message .= '<tr style="background: #8DBFCF;"><th>Containers</th></tr>';
   $message .= print_r($containerArray, true);  
   $message .= '</table>';
   $message .= '<body></html>';

   @mail($to, $subject, $message, $headers);
 ?>

I send an email to myself, and when I view it, the array values are displayed like this:

ContainersArray ( [0] => CMAU123456 [1] => TRLU1234567 ) 

This is not how I want it displayed. I know I have to use a foreach loop to get this to work properly, but I am not sure how or where to start the loop.

Please help.

1
  • You're generating invalid html. You can NOT have "plain" text inside a table that is NOT contained within a row+cell combo, e.g. <table><tr><td>foo</td></tr></table> is ok, <table>foo</table> is invalid. Commented Jan 2, 2014 at 15:58

3 Answers 3

5

Replace:

$message .= print_r($containerArray, true);

With:

foreach($containerArray as $container) {
    $messages .= sprintf('<tr><td>%s</td></tr>', $container);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This answer looks promising. However, upon testing, the email no longer displays any array values. Any thoughts?
Well, everyone else appears to have the same answer as me more or less, so Im fairly sure its right. Has anything changed since you last tested it? Perhaps the data structure of that array isn't quite a simple as printed out. I can't be sure.
3
   $message .= '<html><body>';
   $message .= '<table rules="all" style="border-color: #62c462" cellpadding="10">';
   $message .= '<tr style="background: #8DBFCF;"><th>Containers</th></tr>';
    foreach($ContainersArray as $key=>$val) {
        $message .= '<tr><td>'.$val.'</td></tr>';
    }
   $message .= '</table>';
   $message .= '<body></html>';

Output (layout on web page shows with some style but I am not copying that here).

Containers

CMAU123456

TRLU1234567

Comments

0

Try this:

 $message .= '<tr style="background: #8DBFCF;"><th>Containers</th></tr>';
 foreach ($containerArray as $key => $value) {
    # code... here action to display each value. exemple:
    $message .= '<tr><td>'.$value.'</td></tr>';
 }
 $message .= '</table>';
 $message .= '<body></html>';

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.