การออกรายงานในรูปแบบต่างๆของ PHP
การออกรายงานรูปแบบไฟล์เอกสาร Word, Excel
การแสดงรายงานรูปแบบไฟล์ Word หรือ Excel สามารถ ทำได้โดยการพิมพ์คำสั่งไว้บริเวณด้านบนของหน้าโค๊ด
<?
header("Content-Type: application/msexcel"); // ถ้าต้องการให้ save ออกในรูปแบบใดให้เปลี่่ยนชื่อแอพฯนั้นๆ เช่น msexcel = EXCEL, msword = Word
header('Content-Disposition: attachment; filename="filename.xls"'); // ถ้าต้องการให้ save ไฟล์เป็นชื่อใดให้ใส่ชื่อไฟล์ในช่อง filename.[นามสกุล save] เช่น Word.doc เป็นต้น
?>
ตัวอย่างการใส่โค๊ด
<?
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>
ผลรัน
---------------------------------------------------------------
การออกรายงานในรูปแบบกราฟนั้นจะยกตัวอย่างการใช้ Fusionchart v.3
ก่อนอื่นเลยจะในฐานข้อมูลจะประกอบด้วย field ที่มีการเก็บจำนวนตัวเลข ถ้าไม่มีจะไม่สามารถเรียกค่าได้ ตัวอย่างฐานข้อมูล <<Download>>
ดาวน์โหลด Fusionchart v.3 <<Download>> และ unzip ไว้ในเวป root โดยของผมจะเก็บไว้ที่นี่ C:\AppServ\www\FusionCharts_Evaluation\ จากนั้นก็ลองเรียกดูเลยครับตาม path นี้http://localhost/FusionCharts_Evaluation/Code/PHP/DBExample/Default.php
ถ้ารันได้แบบนี้ก็เรียบร้อย
ถ้ารันได้แบบนี้ก็เรียบร้อย
สาเหตุเกิดจากการที่เราไม่ได้ไปตั้งค่าในหน้า DBConn.php ให้เราเข้าไปใส่โค๊ดให้ถูกต้อง
ตัวอย่าง
[before] <?php
// In this page, we open the connection to the Database
// In this page, we open the connection to the Database
// Our MySQL database (blueprintdb) for the Blueprint Application
// Function to connect to the DB
function connectToDB() {
// These four parameters must be changed dependent on your MySQL settings
$hostdb = 'localhost'; // MySQl host
$userdb = 'root'; // MySQL username
$passdb = 'dddd'; // MySQL password
$namedb = 'fusion-test'; // MySQL database name
$link = mysql_connect ($hostdb, $userdb, $passdb);
if (!$link) {
// we should have connected, but if any of the above parameters
// are incorrect or we can't access the DB for some reason,
// then we will stop execution here
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db($namedb);
if (!$db_selected) {
die ('Can\'t use database : ' . mysql_error());
}
return $link;
}
?>
[after]
<?php
// In this page, we open the connection to the Database
// In this page, we open the connection to the Database
// Our MySQL database (blueprintdb) for the Blueprint Application
// Function to connect to the DB
function connectToDB() {
// These four parameters must be changed dependent on your MySQL settings
$hostdb = 'localhost'; // MySQl host
$userdb = 'root'; // MySQL username
$passdb = '1234'; // MySQL password //แก้ตรงนี้ใหม่
$namedb = 'fusion-test'; // MySQL database name
$link = mysql_connect ($hostdb, $userdb, $passdb);
if (!$link) {
// we should have connected, but if any of the above parameters
// are incorrect or we can't access the DB for some reason,
// then we will stop execution here
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db($namedb);
if (!$db_selected) {
die ('Can\'t use database : ' . mysql_error());
}
return $link;
}
?>
ผลรันเมื่อรันถูกต้อง