1

I have problem to send array from database via email. I´m sure may people have got the same problem. I don´t know how to explain more but here is some script.

$sql="SELECT * FROM tb_xxx WHERE id_prd = '$ref_id_prd'";

        $result=mysql_db_query($dbname,$sql);
        while ($rs=mysql_fetch_array($result)) {

            $ref_id_prd=$rs[ref_id_prd];
                        $prd=$rs[prd];
                        $price=$rs[price];

text="$prd <br>$price";
}


$recipient = $iemail;
$subject = "my subject";

$headers = "From: [email protected] \n";
$headers .= "Reply-To: [email protected] \n";
$headers .= "MIME-Version: 1.0 \n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \n";


   $msg = $text;

   mail($recipient, $subject, $message, $headers);
1
  • write the vars in to a string, send the string. Commented Jan 26, 2012 at 21:20

2 Answers 2

3

You need to accumulate all the array values into a single string for the message body.

$text = "";
while ($rs = mysql_fetch_array($result)) {

        $ref_id_prd=$rs[ref_id_prd];
        $prd=$rs[prd];
        $price=$rs[price];

        // Use .= to append to the current value
        $text .= "$prd <br>$price\n";
}

To send it, use $text as the message body:

mail($recipient, $subject, $text, $headers);

Note, you will need to do additional formatting on the line below to get it looking the way you want in your HTML email:

$text .= "$prd <br>$price\n";

For example, you could make a list of products:

$text = "<ul>\n";
// inside the while loop...
$text .= "<li>$prd: $price</li>\n";
// After the loop, close the list
$text .= "</ul>"

Update: How to build it into an HTML table:

$text = "";

// Open the table:
$text .= "<table>\n";
// Table header...
$text .= "<thead><tr><th>Product</th><th>Price</th></tr></thead>\n";
// Open the table body
$text .= "<tbody>\n";

while ($rs = mysql_fetch_array($result)) {

        $ref_id_prd=$rs[ref_id_prd];
        $prd=$rs[prd];
        $price=$rs[price];

        // Build table rows...
        $text .= "<tr><td>$prd</td><td>$price</td></tr>";
}
// Close the table
$text .= "</table>";
Sign up to request clarification or add additional context in comments.

2 Comments

Could you tell my how to put it in table ?
oh good, now I could finish my project by you, thanks again !
0

Replace your $message variable with $msg

mail($recipient, $subject, $msg, $headers);

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.