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

Thursday, September 21, 2017

0.00 is not empty in empty() php

When we get value like '0.00' from database and we check if it is empty

 if(!empty(0.00)){
      return true;
}

this will return true because, it understand 0.00 is not a number it is a string.


according to php manual a variable is empty if meeting anyone of the following case:

If we want to make it / understand it as a empty then we need to use this
$amount = 0.00;

(float)$amount; 

Now this will return as empty.


  • "" (an empty string)
  • 0 (0 as an integer)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)
  •  
  • Sunday, August 27, 2017

    Check if file / image attach in html form php

    I need to make sure that a file attached through the file input element. I tried various things, including this
    if(count($_FILES) > 0)
    if(empty($_FILES['filename']))
    if(empty($_FILES['filename']['name']))
    but all those option was not working for me.

    is_uploaded_file(). is worked but it's show error when attach multiple file. name='files[]' because it is not support array. 

    Finally the best solution is used for me 
    if($_FILES["filename"]['name'][0] != ''){
        //echo 'file attached';
    }else{
        //echo 'no file attached';
    }

    Total Pageviews