Thursday 20 August 2015

Fetch Array Data from Server,JSON or Mysql Database, Insert it into EXCEL/CSV File and mail it.

Hi Guys,
here we have a complete solution of how to fetch data from server in the JSON format, php array format or java script array format and insert data into excel file or CSV file, and mail it through by PHP MAIL Function.

<?PHP

  $data = array(
    array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25),
    array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18),
    array("firstname" => "James", "lastname" => "Brown", "age" => 31),
    array("firstname" => "Patricia", "lastname" => "Williams", "age" => 7),
    array("firstname" => "Michael", "lastname" => "Davis", "age" => 43),
    array("firstname" => "Sarah", "lastname" => "Miller", "age" => 24),
    array("firstname" => "Patrick", "lastname" => "Miller", "age" => 27)
  );     // these are the manual data in the php array format.
  //echo $dataReceived = json_decode($_REQUEST['data'], TRUE);  //recieve data from json file

  function cleanData(&$str)
  {
    $str = preg_replace("/\t/", "\\t", $str);
    $str = preg_replace("/\r?\n/", "\\n", $str);
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  }

  // file name for download
  $dir = "./excel/";
  $filename = "$dir"."website_data_" . date('Ymd') . ".xls";
  $fd = fopen ($filename, "w");
 
 
 // header("Content-Disposition: attachment; filename=\"$filename\"");
 // header("Content-Type: application/vnd.ms-excel");  //if we want to download file

// if we use json format then change foreach($data as $row)  to foreach($dataReceived as $row)
  $flag = false;
  foreach($data as $row) {
    if(!$flag) {
      // display field/column names as first row
      $filehead = implode("\t", array_keys($row)) . "\n";
 fputs($fd, $filehead);
      $flag = true;
    }
    array_walk($row, 'cleanData');
    $filecontent = implode("\t", array_values($row)) . "\n";
 fputs($fd, $filecontent);

  }
 
    $from_name = "From Name";
$from_mail = "vinod161191@gmail.com";
       $to = "vinod161191@gmail.com";
       $subject = "data in excel format";
     //  $message = $_POST['user_message'];

       //things u need
       $file = $filename;
       $content = chunk_split(base64_encode(file_get_contents($file)));
       $uid = md5(uniqid(time()));  //unique identifier

       //standard mail headers
$header = "From: ".$from_name." <".$from_mail.">\n";
   // $header .= "Reply-To: ".$replyto."\n";
    $header .= "MIME-Version: 1.0\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n\n";
    $emessage= "--".$uid."\n";
    $emessage.= "Content-type:text/plain; charset=iso-8859-1\n";
    $emessage.= "Content-Transfer-Encoding: 7bit\n\n";
    $emessage .= $message."\n\n";
    $emessage.= "--".$uid."\n";
    $emessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\n"; // use different content types here
    $emessage .= "Content-Transfer-Encoding: base64\n";
    $emessage .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
    $emessage .= $content."\n\n";
    $emessage .= "--".$uid."--";
   


       //sending the mail - message is not here, but in the header in a multi part

       if(mail($to,$subject,$emessage,$header)) {
       echo "success";
       }else {
           echo "fail";
       }



  exit;
?>

No comments:

Post a Comment