PHP&MySql การออกรายงานในรูปแบบต่างๆ
Fusionchart+PHP+MySQL
วิธีการแสดงกราฟ Fusionchart v.3 โดยดึงข้อมูลมาจากฐานข้อมูล MySQL โดยใช้ภาษา PHP
step 1. เริ่มจากสร้างฐานข้อมูลที่ชื่อ fusion-test และเรียกฐานข้อมูลที่ได้โหลดจาก Fusionchart ซึ่งเป็นไฟล์สำเร็จไว้ขึ้นมา
จะได้ฐานข้อมูล ดังภาพ
step2.
โหลดกราฟ และ unzip
ไปที่
C:\AppServ\www\FusionCharts_Evaluation\Code\PHP\Includes เพื่อที่เข้าไปแก้ไขโค้ดเชื่อมต่อที่ไฟล์ DBConn.php ดังนี้
$hostdb = 'localhost'; // MySQl host
$userdb = 'root'; // MySQL username
$passdb = '1234'; // MySQL password
$namedb = 'fusion-test'; // MySQL database name
และแก้ไขไฟล์ Default.php ตามนี้
$strQuery
= "select distinct(type) as type_name , sum(amount) as count_amt from
data1 group by type_name";
$strXML
.= "<set label='" . $ors2['type_name'] . "' value='" .
$ors2['count_amt'] . "' />";
และหากต้องการเปลี่ยนกราฟแท่งก็แค่แก้ตรงคำสั่ง
เปลี่ยนจาก Pie3D.swf เป็น Column3D.swf
ตามนี้
echo
renderChart("../../FusionCharts/Column3D.swf", "", $strXML,
"FactorySum", 600, 300, false, false);
ภาพตัวอย่างกราฟแท่ง
ภาพตัวอย่างกราฟ Pie
-----------------------------
การออกรายงานรูปแบบไฟล์เอกสาร Excel
การแสดงรายงานรูปแบบไฟล์ Word หรือ Excel สามารถ ทำได้โดยการพิมพ์คำสั่งไว้บริเวณด้านบนของหน้าโค๊ด
<?
header("Content-Type: application/msexcel"); // ถ้าต้องการให้ save ออกในรูปแบบใดให้เปลี่่ยนชื่อแอพฯนั้นๆ เช่น msexcel = EXCEL, msword = Word
header('Content-Disposition: attachment; filename="filename.xls"'); // ถ้าต้องการให้ save ไฟล์เป็นชื่อใดให้ใส่ชื่อไฟล์ในช่อง filename.[นามสกุล save] เช่น Excel.xls เป็นต้น
?>
ตัวอย่างการใส่โค๊ด
<?
header("Content-Type: application/msexcel"); // ใส่เหนือแท็ก HTML
header('Content-Disposition: attachment; filename="filename.xls"');
?>
<html>
<head>
<title>Customer</title>
</head>
<body background="bg-1.jpg"><center>
<?
include("connect2.php");
mysql_connect( $host,$username,$password) or die ("ไม่สามารถติดต่อกับฐานข้อมูลได้ ");
mysql_select_db($db) or die("ไม่พบฐานข้อมูล");
$sql="SELECT e.CustomerID, e.Name, e.Email, e.Used, e.Budget, d.CountryName
FROM customer e
JOIN country d ON e.CountryCode = d.CountryCode";
$db_query=mysql_db_query($db,$sql);
$num_rows=mysql_num_rows($db_query);
?>
<br/>
<br/>
<table border="5" bordercolor="#B9B9B9" width="900" cellpadding='0' cellspacing='0'><tr bgcolor="#D8D8D8">
<th>CustomerID</th>
<th>Name</th>
<th>Email</th>
<th>Country</th>
<th>Budget</th>
<th>Used</th>
</tr>
<?
$a=0;
while($a < $num_rows)
{
$result = mysql_fetch_array($db_query);
$a1=$result[CustomerID];
$a2=$result[Name];
$a3=$result[Email];
$a4=$result[CountryName];
$a5=$result[Budget];
$a6=$result[Used];
echo "<tr bgcolor='#F0F0F0'>" .
"<td align='center'>" . $a1. "</td>" .
"<td align='center'>" . $a2 . "</td>" .
"<td align='center'>" . $a3 . "</td>" .
"<td align='center'>" . $a4 . "</td>" .
"<td align='center'>" . $a5 . "</td>" .
"<td align='center'>" . $a6 . "</td>" .
"</tr>";
$a++;
}
mysql_close();
?>
</table>
</body>
</html>



