Have been working on this code for some time (thanks to those on this board who helped me troubleshoot my class code!), and am now trying to get another feature added to replace a string of text (pulled from a table in a database) to be replaced with another value (that is currently a static value).
First off, here is the class code from file EmailTemplate.class.php. What this code is supposed to do is access the specified database, pulling the indicated template (#1) from the table. In that table, there is a field that contains fieldnames (First-Name, Last-Name, Purchase-Price, etc; separated by |). I am gathering those fieldnames into an array using preg_split to break the array values on the "|"
Once that is done in the first function (LoadEmailTemplate), the second function basically runs a check to ensure that proper fieldnames were selected.
The third function is getText(). This function needs to take the fieldname values from the array (First-Name, Last-Name, Purchase-Price, etc), and replace them with approriate values from another location...in this case we are passing static "test" values in the email_templates_sample.php file ($etmp->setParam('First-Name','Last-Name').
Currently when I run the email_template_sample.php file, it simply displays a blank window. No errors, no results. If this were working properly, it should be displaying John Smith $9.95.
Code for both files is below. Any ideas on what I'm not doing right? I'm pretty sure the problem is in the getText() function in how I'm handling the array and search/replace functions on the array string values. I've never done this type of thing before, so I'm hoping someone has some experience and can get me in the right direction! If there is something elsewhere in the code that may be causing me problems...please feel free to point out how I can improve...I'm always trying to learn better ways to do things.
Thanks so much for any help.
EmailTemplate.class.php
<?
ini_set('display_errors', 1); // turn off for production version
error_reporting(E_ALL);
class EmailTemplate {
var $params;
var $name;
var $value;
var $search;
var $replace;
var $fields=null;
var $text=null;
var $FieldArray=null;
function LoadEmailTemplate($templateID){
// FIND AND CREATE THE ARRAY OF FIELDLIST ENTRIES FROM TABLE
$db = Database::getInstance();
$sql = "
SELECT
*
FROM
email_templates
WHERE
ID='$_GET[templateID]'
ORDER BY
Name
";
$result = $db->Execute($sql) or die($sql."<p>".$db->ErrorMsg());
if($row=$result->FetchRow()) {
// SPLIT FIELDS ON PIPE AND ANY SPACE CHARACTERS
$FieldArray = preg_split('/[\|\s]/',$row['FieldList'],-1, PREG_SPLIT_NO_EMPTY);
} else {
die('Invalid Template ID');
}
}
function setParam($name,$value)
{
if(!isset($this->fields{$this->name}))
{
// trigger error
}
$this->params[$this->name]=$this->value;
}
/*
function setParam($name, $value)
{
$this->params[$name] = $value;
}
*/
function getText()
{
if (is_array($this->fields)) {
foreach ($FieldArray as $field=>$inner_fields) {
$FieldArray[$field]=str_replace_array($name, $value, $inner_fields);
}
} else {
$FieldArray=str_replace($this->name, $this->value, $this->fields);
}
return $FieldArray;
}
}
?>
email_template_sample.php
<?
$_GET['templateID'] = "1";
include("EmailTemplate.class.php");
$etmp = new EmailTemplate(1);
$etmp->setParam('First-Name','John');
$etmp->setParam('Last-Name','Smith');
$etmp->setParam('Purchase-Price','$9.95');
print $etmp->getText();
?>