phpmailer to send html table in message

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adelgirl
Forum Newbie
Posts: 1
Joined: Thu Aug 26, 2010 10:44 pm

phpmailer to send html table in message

Post by adelgirl »

I am trying to show a table in a html email using phpmailer. I am new to php and mysql (and html for that matter lol)... I can get it to show one record fine, but when I get to the loops and array statement (???), I keep getting server 500 records, (i believe i may not be putting an end to the loop perhaps) but have not posted any of my attempts and I am hoping someone will head me in the right direction as to how to get this to work.

I have included snippets of my code that does work (and emails one record just how i want it to go) and the steps taken to get there.

1) $row->id gets sent to email function (with other data) when user presses save on a form.

2) Here is mysql statement to get the data I want to show in the table from the database

Code: Select all

        //Get Service Details
                $row->id = strip_tags($_POST['id']);
                $query = "SELECT * FROM #__jservicerelation INNER JOIN #__jservices on #__jservicerelation.serviceid = #__jservices.id WHERE invoiceid=$row->id";
			$database->setQuery($query);
		        $appliedservices = $database->loadAssoc();
               
               $num=mysql_numrows($appliedservices);


               $qty =  $appliedservices['quantity'];
               $desc = $appliedservices['productname'];
               $list = $appliedservices['listprice'];            	
               $totalcost = $appliedservices['listprice'] * $appliedservices['quantity'];
3. here is where i am having trouble and could use a point in the right direction

4. To create a table with a single record - I am using this and it works perfect. I need to obviously change this to include subsequent lines of the string

Code: Select all

$message .= '<table rules="all" style="border: 1px solid #DDD;" cellpadding="10">';
$message .= "<tr><td><strong>Qty</strong> </td><td><strong>Description</strong> </td><td><strong>Net Price</strong> </td><td><strong>Total</strong> </td></tr>";
$message .= "<tr><td>" . strip_tags($qty) . "</td><td>" . strip_tags($desc) . "</td><td>" . strip_tags($list) . "</td><td>" . strip_tags($totalcost) . "</td></tr>";		                    
$message .= "</table>";
thanks in advance!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: phpmailer to send html table in message

Post by social_experiment »

Im not familiar with your code so i will explain with the example i created. Firstly you get the records that you want the user to see

Code: Select all

<?php

$query = "SELECT field1, $field2, $field3 FROM table";
$sql = mysql_query($query);

// 'id' is normally a primary key
$countQuery = "SELECT COUNT(id) FROM table";
$countSql = mysql_query($countQuery);
$countArray = mysql_fetch_array($countSql);
$rows = $countArray[0];

// iteration value
$i = 0;

while ($sqlArray = mysql_fetch_array($sql)) {
	// create an associative array with all your information
	$arrayName[$i]['fieldName1'] = $sqlArray['field1'];
	$arrayName[$i]['fieldName2'] = $sqlArray['field2'];
	$arrayName[$i]['fieldName3'] = $sqlArray['field3'];
	
	// the iteration value is increased so the next time the while loop
	// is run, you will have a new associative array
	$i++;
} ?>
Within the while loop create the array that holds the information. The value of $i at each iteration will create an associative array each time the loop is run so each of the records ends up in their own associative array with fieldName1, fieldName2 and fieldName3 as keys, respectively.

Next is the function that parses the array and gets the values from it into a variable that can be printed.

Code: Select all

<?php
function displayResultsAsTable($resultsArray) {
// argument must be an array
if (is_array($resultsArray)) {
	foreach ($resultsArray as $key => $value) {
		$val .= '<tr>';
			foreach ($value as $f_key => $f_val) {
				$val .= '<td>'. $f_val .'</td>';
			}
		$val .= '</tr>';
		}
	}
	return $val;
}?>
This returns $val with all your records wrapped in <tr>, </tr>, <td> and </td> tags. Im not sure how phpmailer works but if you then place the value received from displayResultsAsTable() inside the body of the message it sends the value as a table. I used the mail() function to get the table sent, below is the code. The code used in your script where you create $message, looks similar to the code used to create $body in my script. I've purposefully left out the mailing part of the script but needless to say it sends the information to you, in a html mail as a table.

Code: Select all

<?php
 $headers = 'MIME-Version: 1.0' . "\r\n";
 $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .= "From: a@test.addy" . "\r\n";
 $subject = 'Testing the tables';
 $body = "<html>";
 $body .= "<body>";
 $body .= '<table>';
 $body .= '<tr><th>Term</th><th>Page</th><th>Relation</th></tr>';
 $body .= displayResultsAsTable($terms);
 $body .= '</table>';
 $body .= "</body></html>";
?>
Attached is the example script. hope this helps
Attachments
demoScript.zip
(835 Bytes) Downloaded 328 times
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply