Sunday, October 1, 2017

Store php variable in mysql / dynamic email, SMS template

This is a good tutorial if we want to create dynamic Email Template/ Dynamic SMS Template or dynamic content using php mysql. Storing php variable in content and save to mysql database. when fetching data from database replace variable with value dynamically.

 First create a table as your require.  Suppose we want to store this kind of sms template in datasbse

 <p>  
 Hello : [FULL_NAME] <br/>   
 username: [USER_NAME] <br/>   
 email : [USER_EMAIL] <br/>  
 website : [SITE_URL]  
 </p>  

Now we can fetch this data from database and assign in a php variable

 $content = $row['content'];  
 //replace template var with value  
 $token = array(  
   'FULL_NAME' => 'Sharif Ahmed',  
   'USER_NAME' => 'winsharif',  
   'SITE_URL' => 'http://www.esteemcorporation.com',  
   'USER_EMAIL'=> 'winsharif@test.com'  
 );  
 $pattern = '[%s]';  
 foreach($token as $key=>$val){  
   $repVar[sprintf($pattern,$key)] = $val;  
 }  
 $SMSContent = strtr($content,$repVar);  
 echo $SMSContent;  

/////////////////////////////////////Output will be //////////////////////
Hello : Sharif Ahmed 
username: winsharif
email : http://www.esteemcorporation.com 
website :  winsharif@test.com

Total Pageviews