I'm just starting to learn PHP and I'm trying to send my table via email. I am receiving the email but the body just says Array. I dont know where to go from here, please help.

This is how my table is being displayed, and the send mail function in there as well

<form action="assign.php" method="post"><?php

if(is_array($result)){
    echo '
    <fieldset> <legend>Assign Ticket</legend> <div>Changes will affect updated rows only.</div> <p></p> <table width=auto cellpadding=1px cellspacing=0px border=1 align=center id=assign> <thead> <tr>';      

    // column comment from DB as column header
    foreach($result[0] as $key => $val){
        echo '<th align=center>'.$colcomments[$key].'</th>';
        }
    echo '
            </tr> </thead> <tbody>';
    foreach($result as $row => $info){
    echo '<tr>';
    foreach($info as $key => $val){
    if($key=='id'){
    echo '<td title="'.$colcomments[$key].'">'.$val.'.<input type="hidden" name="'.$key.'['.$info['id'].']" value="'.$val.'" id="rowid_'.$val.'" /></td>';
         }
    else {
    echo '<td title="'.$colcomments[$key].'"><input type="text" name="'.$key.'['.$info['id'].']" value="'.$val.'" /></td>';
         }
         }
    echo '</tr>'; 
         }
    echo '
            </tbody> </table> </fieldset>';

    if($result) {
        $Body = "<html>\n"
            . "<head>\n"
            . "</head>\n"
            . "<body>\n"
            . $result
            . "</body>\n"
            . "</html>\n";
    //Setting up Mail
        $mail = new PHPMailer();
        if (EMAIL_USE_SMTP) {
            // Set mailer to use SMTP
            $mail->IsSMTP();
            //useful for debugging, shows full SMTP errors
            $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
            // Enable SMTP authentication
            $mail->SMTPAuth = EMAIL_SMTP_AUTH;
            // Enable encryption, usually SSL/TLS
            if (defined(EMAIL_SMTP_ENCRYPTION)) {
                $mail->SMTPSecure = EMAIL_SMTP_ENCRYPTION;
            }
            // Specify host server
            $mail->Host = EMAIL_SMTP_HOST;
            $mail->Username = EMAIL_SMTP_USERNAME;
            $mail->Password = EMAIL_SMTP_PASSWORD;
            $mail->Port = EMAIL_SMTP_PORT;
        } else {
            $mail->IsMail();
        }
        $mail->From = EMAIL_FROM_ADDRESS;
        $mail->FromName = EMAIL_FROM_NAME;
        $mail->AddAddress('sample.test@domain.COM');
        $mail->Subject = 'Ticket Assignment - ';
        $mail->WordWrap = 100;
        $mail->IsHTML(true);
        $mail->Body = $Body;
        $mail->Send();
    }
}

?> <fieldset> <legend>Select Date</legend> <div>Select Date from and Date to</div> <p></p> <input type="date" name="from" id="from" value="<?=$date['from']; ?>" /> <input type="date" name="to" id="to" value="<?=$date['to']; ?>" /> <div><input type="submit" value="Submit" /></div> </fieldset> </form>

Here is the smtp debug result that I am getting,

Notice: Array to string conversion in C:\*\assign.php on line 260

Which refers to this,

. "</body>\n"

Because $result is array - do not concatenate as a string

Yeah and I tried doing this

if($result) {
        $Body = "<html>\n"
            . "<head>\n"
            . "<style>\n"
            . file_get_contents('style.css')
            . "</style>\n"
            . "</head>\n"
            . "<body>\n" 
            . json_encode($result)
            . "</body>\n"
            . "</html>\n";

I'm seeing other messages now in the body but it does not seem to have the same format as how i sees it in my browser, and all I get is this with no formats at all

[{"id":"21","testid1":"test7","testid2":"test7","summary":"t‌​est7","type":"test7"‌​,"tested_by":"TEST",‌​"start_date":"2017-0‌​1-03","Tester":""}]

Create function for convert array to html table instead of json_encode

I tried to do it this way as well but just getting an error,

if($result) {
        $Body = "<html>\n"
            . "<head>\n"
            . "<style>\n"
            . file_get_contents('style.css')
            . "</style>\n"
            . "</head>\n"
            . "<body>\n"
            . foreach ($result as $l => $val){
              ?>
                <tr>
                <td><?php echo $l[0] ;?> </td>
                <td><?php echo $l[1] ;?> </td>
                <td><?php echo $l[2] ;?> </td>
                <?php
                }
            . "</body>\n"
            . "</html>\n";

This is the error,

Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in C:\*\assign.php on line 259

and it refers to

. foreach ($result as $l => $val){

Totally at lost here...

Create it as function e.g.

function two_dim_array_to_html_table($arr){
    $ret = "<table border='1'>\n";
    foreach($arr as $row){
        $ret .= "\t<tr>\n";
        foreach($row as $column){
            $ret .= "\t\t<td>".$column."</td>\n";
            }
        $ret .= "\t</tr>\n";
        }
    $ret .= "<table>\n";
    return $ret;
    }

and call it instead of json_encode

$Body = "<html>\n"
    . "<head>\n"
    . "<style>\n"
    . file_get_contents('style.css')
    . "</style>\n"
    . "</head>\n"
    . "<body>\n" 
    . two_dim_array_to_html_table($result)
    . "</body>\n"
    . "</html>\n";

Okay, that was a good start. But I cant seem to figure out on how I can add the Header as I want to email exactly what is in my browser.

Put style attributes directly in to the HTML elements instead of CSS stylesheet

The column header from the table was just fetch from the column comment from the db. I've also removed the style.css as its not doing any good, i'll try to worry about the style later. I tried to add foreach to show the columns header but getting nowhere,

function two_dim_array_to_html_table($arr){
    $ret = "<table border='1'>\n";
    foreach($arr as $row){
        $ret .= "\t<tr>\n";
        foreach($row as $header){
            $ret .= "\t<th>".$header."</th>\n";
        foreach($row as $column){
            $ret .= "\t\t<td>".$column."</td>\n";
            }
        $ret .= "\t</th>\n";    
        $ret .= "\t</tr>\n";
        }
        }
    $ret .= "<table>\n";
    return $ret;
    }

Replace function and pass array $colcomments as second argument. Then before foreach($arr as $row) implement column headers

I tried this code, and this time I am getting an Array message on the header instead of the column header names. Should I be creating another function, If I'm going to create another function for the header, how am I supposed to call it?

function two_dim_array_to_html_table($arr){
    $ret = "<table border='1'>\n";
    foreach($arr as $colcomments) {
        $ret .= "\t<th>".$colcomments."</th>\n";
    foreach($arr as $row){
        $ret .= "\t<tr>\n";
        foreach($row as $column){
            $ret .= "\t\t<td>".$column."</td>\n";
            }
        $ret .= "\t</tr>\n";
        $ret .= "\t</th>\n";
        }
        }
    $ret .= "<table>\n";
    return $ret;
    }

You forgot to add the second input parameter to function. Table row tags <tr> open and close for column headers is missing. Lines 11. and 12. should be before line 5.

So I got this now with two params but I am getting an error,

function two_dim_array_to_html_table($arr, $colcomments){
    $ret = "<table border='1' width='auto' cellpadding='1px' cellspacing='0px' align='center'>\n";
    foreach($colcomments as $row) {
        $ret .= "\t<tr>\n";
        foreach ($row as $header){
        $ret .= "\t\t<th>".$header."</th>\n";
        $ret .= "\t</th>\n";
        }
        $ret .= "\t</tr>\n";
    foreach($arr as $row){
        $ret .= "\t<tr>\n";
        foreach($row as $column){
            $ret .= "\t\t<td>".$column."</td>\n";
            }

        $ret .= "\t</tr>\n";

        }

    $ret .= "<table>\n";
    return $ret;
    }
    }

Warning: Missing argument 2 for two_dim_array_to_html_table(), called in C:*\assign.php on line 281 and defined in C:*\assign.php on line 251

Notice: Undefined variable: colcomments in C:*\assign.php on line 253

Which is this respectively

function two_dim_array_to_html_table($arr, $colcomments){

foreach($colcomments as $row) {

You forgot to pass second input parameter when you call function

I dont think I'm following that i forgot to pass second input parameter when i call the function, i thought i'm doing that already here,

function two_dim_array_to_html_table($arr, $colcomments){
$ret = "<table border='1' width='auto' cellpadding='1px' cellspacing='0px' align='center'>\n";
foreach($arr as $colcomments) {
    $ret .= "\t<tr>\n";
    foreach ($row as $header){
    $ret .= "\t\t<th>".$header."</th>\n";
    $ret .= "\t</th>\n";
    }
    $ret .= "\t</tr>\n";
foreach($arr as $row){
    $ret .= "\t<tr>\n";
    foreach($row as $column){
        $ret .= "\t\t<td>".$column."</td>\n";
        }

    $ret .= "\t</tr>\n";

    }

$ret .= "<table>\n";

On the call function

    $Body = "<html>\n"
        . "<head>\n"
        . "<style>\n"
        . file_get_contents('style.css')
        . "</style>\n"
        . "</head>\n"
        . "<body>\n" 
        . two_dim_array_to_html_table($result, $colcomments)
        . "</body>\n"
        . "</html>\n";

but inside function when you generate column headers need foreach first row of array to get similar set of indexes because array $colcomments may contain more key pairs

Yeah sorry, that was added already and i'm left with this error

Warning: Invalid argument supplied for foreach() in C:*_assign.php on line 255

Which is

foreach ($row as $colcomments){

And i dont know what to supply into it.

In to the line 5 you are trying to handle $row but it not defined in this step

function two_dim_array_to_html_table($arr, $header){
    $ret = "<table border='1'>\n";
    $ret .= "\t<tr>\n";
    foreach($arr[0] as $key => $val){ // use set of $key only but $val do not use
        $ret .= "\t\t<th>".$header[$key]."</th>\n";
        }
    $ret .= "\t</tr>\n";
    foreach($arr as $row){
        $ret .= "\t<tr>\n";
        foreach($row as $column){
            $ret .= "\t\t<td>".$column."</td>\n";
            }
        $ret .= "\t</tr>\n";
        }
    $ret .= "<table>\n";
    return $ret;
    }

Thank you very much, that was just really the best that i can get. I'm a newbie in PHP and I could say i got better today with the tough exercise that you were asking me to try and figure it out myself. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.