I'm building a newsletter system and I've added a feature to allow the user to preview the content of the newsletter before they send it. I've added keywords in the newsletter template which is a html file and then I loop through the database and replace the keywords with the table content.
this is my code:
function getNewsletter(){
$template='template.html';
if(file_exists($template)){
$handle=fopen($template,"r");
//fread($handle, filesize($template));
$templateContent=file_get_contents($template);
//loop through the content and replace the topics
$sql="SELECT t.topic,a.topic_id,a.content FROM topics t,articles a WHERE t.id=a.topic_id AND newsletter_id=1";
$result=mysql_query($sql) or die("Error executing query ".mysql_error());
while($row=mysql_fetch_assoc($result)){
$topic=strtolower($row['topic']);
$article=$row['content'];
$preview=str_replace("#".$topic."#",$article,$templateContent);
}
return $preview;
fclose($handle);
}
}
My sample template:
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td>MEDICINE</td>
</tr>
<tr>
<td>#medicine#</td>
</tr>
<tr>
<td>EVENTS</td>
</tr>
<tr>
<td>#events#</td>
</tr>
<tr>
<td>BUSINESS</td>
</tr>
<tr>
<td>#business#</td>
</tr>
</table>
The problem I'm having is that when I click on the preview link only one row of the content is replaced yet all the topic content exists and despite the fact that there is a loop that should replace the text with the content.