Learning & Integrating web technology and code help directory

How to use PHP & Chart/Graph - Generate Chart and Send Email Att achm ent Attachment Atta ch ment Attachment

No comments
How to use PHP & Chart/Graph - Generate Chart and Send Email Attachment This learn / tutorial php programming how to using  PHP Create Chart/Graph - Generate Chart and Send Email Attachment
ShotDev Focus:
- PHP  & Create Chart/Graph - Generate Chart and Send Email Attachment
Example
php_chart_mail.php

  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7.   
  8. //*** Connect to MySQL Database ***//  
  9. $objConnect = mysql_connect("localhost","root","root"or die(mysql_error());  
  10. $objDB = mysql_select_db("mydatabase");  
  11. $strSQL = "SELECT * FROM customer";  
  12. $objQuery = mysql_query($strSQL);  
  13. if($objQuery)  
  14. {  
  15. //*** Get Document Path ***//  
  16. $strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp  
  17.   
  18. //*** File Name Gif,Jpeg,... ***//  
  19. $FileName = "MyXls/MyChart.Gif";  
  20. $Ext = "Gif";  
  21.   
  22. //*** Excel Name ***//  
  23. $XlsName = "MyXls/MyChart.xls";  
  24.   
  25. //*** Connect to Excel.Application ***//  
  26. $xlApp = new COM("Excel.Application");  
  27. $xlBook = $xlApp->Workbooks->Add();  
  28.   
  29. $intStartRows = 2;  
  30. $intEndRows = mysql_num_rows($objQuery)+($intStartRows-1);  
  31.   
  32. $xlSheet = $xlBook->Worksheets(1);  
  33.   
  34. $xlApp->Application->Visible = False;  
  35.   
  36. //*** Delete Sheet (2,3) - Sheet Default ***//  
  37. $xlBook->Worksheets(2)->Select;  
  38. $xlBook->Worksheets(2)->Delete;  
  39. $xlBook->Worksheets(2)->Select;  
  40. $xlBook->Worksheets(2)->Delete;  
  41.   
  42. //*** Sheet Data Rows ***//  
  43. $xlBook->Worksheets(1)->Name = "MyReport";  
  44. $xlBook->Worksheets(1)->Select;  
  45.   
  46. $xlBook->ActiveSheet->Cells(1,1)->Value = "Customer Name";  
  47. $xlBook->ActiveSheet->Cells(1,1)->Font->Name = "Tahoma";  
  48. $xlBook->ActiveSheet->Cells(1,1)->BORDERS->Weight = 1;  
  49. $xlBook->ActiveSheet->Cells(1,1)->Font->Size = 10;  
  50. $xlBook->ActiveSheet->Cells(1,1)->MergeCells = True;  
  51.   
  52. $xlBook->ActiveSheet->Cells(1,2)->Value = "Budget";  
  53. $xlBook->ActiveSheet->Cells(1,2)->BORDERS->Weight = 1;  
  54. $xlBook->ActiveSheet->Cells(1,2)->Font->Name = "Tahoma";  
  55. $xlBook->ActiveSheet->Cells(1,2)->Font->Size = 10;  
  56. $xlBook->ActiveSheet->Cells(1,2)->MergeCells = True;  
  57.   
  58. $xlBook->ActiveSheet->Cells(1,3)->Value = "Used";  
  59. $xlBook->ActiveSheet->Cells(1,3)->BORDERS->Weight = 1;  
  60. $xlBook->ActiveSheet->Cells(1,3)->Font->Name = "Tahoma";  
  61. $xlBook->ActiveSheet->Cells(1,3)->Font->Size = 10;  
  62. $xlBook->ActiveSheet->Cells(1,3)->MergeCells = True;  
  63.   
  64. $i = 0;  
  65. While($result = mysql_fetch_array($objQuery))  
  66. {  
  67. $xlBook->ActiveSheet->Cells($intStartRows+$i,1)->Value = $result["Name"];  
  68. $xlBook->ActiveSheet->Cells($intStartRows+$i,2)->Value = $result["Budget"];  
  69. $xlBook->ActiveSheet->Cells($intStartRows+$i,3)->Value = $result["Used"];  
  70. $xlBook->ActiveSheet->Cells($intStartRows+$i,2)->NumberFormat = "$#,##0.00";  
  71. $xlBook->ActiveSheet->Cells($intStartRows+$i,3)->NumberFormat = "$#,##0.00";  
  72. $i++;  
  73. }  
  74. //*** End Data Rows ***//  
  75.   
  76. //*** Charts Properties ***//  
  77. $objRange = $xlBook->Sheets("MyReport")->UsedRange;  
  78. $objRange->Select;  
  79.   
  80. $xlBook->Charts->Add();  
  81.   
  82. //*** Set Localtion Sheet ***//  
  83. $xlBook->ActiveChart->Location(2,"MyReport");  
  84.   
  85. //*** Charts Properties ***//  
  86. $xlBook->ActiveChart->ChartType = 98;  
  87. $xlBook->ActiveChart->HasLegend = True;  
  88. $xlBook->ActiveChart->HasTitle = 1;  
  89. $xlBook->ActiveChart->ChartTitle->Text = "Customer Report";  
  90.   
  91. //*** Legend Properties ***//  
  92. $xlBook->ActiveChart->Legend->Font->Name = "Arial";  
  93. $xlBook->ActiveChart->Legend->Font->Size = 5;  
  94.   
  95. //*** Set Area & Location  ***//  
  96. $xlBook->ActiveSheet->Shapes("Chart 1")->IncrementLeft(20);  
  97. $xlBook->ActiveSheet->Shapes("Chart 1")->IncrementTop(-97.5);  
  98.   
  99. //'*** Set Height & Width ***'  
  100. $xlBook->ActiveSheet->Shapes("Chart 1")->ScaleHeight(2.0, 0,0);  
  101. $xlBook->ActiveSheet->Shapes("Chart 1")->ScaleWidth(1.5, 0,0);  
  102.   
  103. //*** Save Charts ***//  
  104. @unlink($strPath."/".$FileName);  
  105. $xlApp->ActiveChart->Export($strPath."/".$FileName,$Ext);  
  106.   
  107. //*** Save Excel ***//  
  108. @unlink($strPath."/".$XlsName);  
  109. $xlBook->SaveAs($strPath."/".$XlsName);  
  110.   
  111. $xlApp->Application->Quit;  
  112. }  
  113.   
  114. //*************** Send Email ***************//  
  115.   
  116. $strTo = "member@shotdev.com";  
  117. $strSubject = "Charts Report";  
  118. $strMessage = "Download MyChart.Gif for Excel Report";  
  119.   
  120. //*** Uniqid Session ***//  
  121. $strSid = md5(uniqid(time()));  
  122.   
  123. $strHeader = "";  
  124. $strHeader .= "From: Mr.Weerachai Nukitram<webmaster@shotdev.com>\nReply-To: webmaster@shotdev.com\n";  
  125. $strHeader .= "Cc: Mr.Surachai Sirisart<surachai@shotdev.com>";  
  126. $strHeader .= "Bcc: webmaster@shotdev.com";  
  127.   
  128. $strHeader .= "MIME-Version: 1.0\n";  
  129. $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
  130. $strHeader .= "This is a multi-part message in MIME format.\n";  
  131.   
  132. $strHeader .= "--".$strSid."\n";  
  133. $strHeader .= "Content-type: text/html; charset=windows-874\n"// or UTF-8 //  
  134. $strHeader .= "Content-Transfer-Encoding: 7bit\n\n";  
  135. $strHeader .= $strMessage."\n\n";  
  136.   
  137. $strContent1 = chunk_split(base64_encode(file_get_contents("MyXls/MyChart.Gif")));  
  138. $strHeader .= "--".$strSid."\n";  
  139. $strHeader .= "Content-Type: application/octet-stream; name=\"MyChart.Gif\"\n";  
  140. $strHeader .= "Content-Transfer-Encoding: base64\n";  
  141. $strHeader .= "Content-Disposition: attachment; filename=\"MyChart.Gif\"\n\n";  
  142. $strHeader .= $strContent1."\n\n";  
  143.   
  144. $flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error //  
  145. if($flgSend)  
  146. {  
  147. echo "Charts Generated & Email Sending.";  
  148. }  
  149. else  
  150. {  
  151. echo "Cannot send mail.";  
  152. }  
  153. ?>  
  154. </body>  
  155. </html>  
Create a php file and save to path root-path/myphp/

No comments :

Post a Comment