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
<html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <? //*** Connect to MySQL Database ***// $objConnect = mysql_connect("localhost","root","root") or die(mysql_error()); $objDB = mysql_select_db("mydatabase"); $strSQL = "SELECT * FROM customer"; $objQuery = mysql_query($strSQL); if($objQuery) { //*** Get Document Path ***// $strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp //*** File Name Gif,Jpeg,... ***// $FileName = "MyXls/MyChart.Gif"; $Ext = "Gif"; //*** Excel Name ***// $XlsName = "MyXls/MyChart.xls"; //*** Connect to Excel.Application ***// $xlApp = new COM("Excel.Application"); $xlBook = $xlApp->Workbooks->Add(); $intStartRows = 2; $intEndRows = mysql_num_rows($objQuery)+($intStartRows-1); $xlSheet = $xlBook->Worksheets(1); $xlApp->Application->Visible = False; //*** Delete Sheet (2,3) - Sheet Default ***// $xlBook->Worksheets(2)->Select; $xlBook->Worksheets(2)->Delete; $xlBook->Worksheets(2)->Select; $xlBook->Worksheets(2)->Delete; //*** Sheet Data Rows ***// $xlBook->Worksheets(1)->Name = "MyReport"; $xlBook->Worksheets(1)->Select; $xlBook->ActiveSheet->Cells(1,1)->Value = "Customer Name"; $xlBook->ActiveSheet->Cells(1,1)->Font->Name = "Tahoma"; $xlBook->ActiveSheet->Cells(1,1)->BORDERS->Weight = 1; $xlBook->ActiveSheet->Cells(1,1)->Font->Size = 10; $xlBook->ActiveSheet->Cells(1,1)->MergeCells = True; $xlBook->ActiveSheet->Cells(1,2)->Value = "Budget"; $xlBook->ActiveSheet->Cells(1,2)->BORDERS->Weight = 1; $xlBook->ActiveSheet->Cells(1,2)->Font->Name = "Tahoma"; $xlBook->ActiveSheet->Cells(1,2)->Font->Size = 10; $xlBook->ActiveSheet->Cells(1,2)->MergeCells = True; $xlBook->ActiveSheet->Cells(1,3)->Value = "Used"; $xlBook->ActiveSheet->Cells(1,3)->BORDERS->Weight = 1; $xlBook->ActiveSheet->Cells(1,3)->Font->Name = "Tahoma"; $xlBook->ActiveSheet->Cells(1,3)->Font->Size = 10; $xlBook->ActiveSheet->Cells(1,3)->MergeCells = True; $i = 0; While($result = mysql_fetch_array($objQuery)) { $xlBook->ActiveSheet->Cells($intStartRows+$i,1)->Value = $result["Name"]; $xlBook->ActiveSheet->Cells($intStartRows+$i,2)->Value = $result["Budget"]; $xlBook->ActiveSheet->Cells($intStartRows+$i,3)->Value = $result["Used"]; $xlBook->ActiveSheet->Cells($intStartRows+$i,2)->NumberFormat = "$#,##0.00"; $xlBook->ActiveSheet->Cells($intStartRows+$i,3)->NumberFormat = "$#,##0.00"; $i++; } //*** End Data Rows ***// //*** Charts Properties ***// $objRange = $xlBook->Sheets("MyReport")->UsedRange; $objRange->Select; $xlBook->Charts->Add(); //*** Set Localtion Sheet ***// $xlBook->ActiveChart->Location(2,"MyReport"); //*** Charts Properties ***// $xlBook->ActiveChart->ChartType = 98; $xlBook->ActiveChart->HasLegend = True; $xlBook->ActiveChart->HasTitle = 1; $xlBook->ActiveChart->ChartTitle->Text = "Customer Report"; //*** Legend Properties ***// $xlBook->ActiveChart->Legend->Font->Name = "Arial"; $xlBook->ActiveChart->Legend->Font->Size = 5; //*** Set Area & Location ***// $xlBook->ActiveSheet->Shapes("Chart 1")->IncrementLeft(20); $xlBook->ActiveSheet->Shapes("Chart 1")->IncrementTop(-97.5); //'*** Set Height & Width ***' $xlBook->ActiveSheet->Shapes("Chart 1")->ScaleHeight(2.0, 0,0); $xlBook->ActiveSheet->Shapes("Chart 1")->ScaleWidth(1.5, 0,0); //*** Save Charts ***// @unlink($strPath."/".$FileName); $xlApp->ActiveChart->Export($strPath."/".$FileName,$Ext); //*** Save Excel ***// @unlink($strPath."/".$XlsName); $xlBook->SaveAs($strPath."/".$XlsName); $xlApp->Application->Quit; } //*************** Send Email ***************// $strTo = "member@shotdev.com"; $strSubject = "Charts Report"; $strMessage = "Download MyChart.Gif for Excel Report"; //*** Uniqid Session ***// $strSid = md5(uniqid(time())); $strHeader = ""; $strHeader .= "From: Mr.Weerachai Nukitram<webmaster@shotdev.com>\nReply-To: webmaster@shotdev.com\n"; $strHeader .= "Cc: Mr.Surachai Sirisart<surachai@shotdev.com>"; $strHeader .= "Bcc: webmaster@shotdev.com"; $strHeader .= "MIME-Version: 1.0\n"; $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n"; $strHeader .= "This is a multi-part message in MIME format.\n"; $strHeader .= "--".$strSid."\n"; $strHeader .= "Content-type: text/html; charset=windows-874\n"; // or UTF-8 // $strHeader .= "Content-Transfer-Encoding: 7bit\n\n"; $strHeader .= $strMessage."\n\n"; $strContent1 = chunk_split(base64_encode(file_get_contents("MyXls/MyChart.Gif"))); $strHeader .= "--".$strSid."\n"; $strHeader .= "Content-Type: application/octet-stream; name=\"MyChart.Gif\"\n"; $strHeader .= "Content-Transfer-Encoding: base64\n"; $strHeader .= "Content-Disposition: attachment; filename=\"MyChart.Gif\"\n\n"; $strHeader .= $strContent1."\n\n"; $flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error // if($flgSend) { echo "Charts Generated & Email Sending."; } else { echo "Cannot send mail."; } ?> </body> </html>
Create a php file and save to path root-path/myphp/