twitter
rss

การออกรายงานในรูปแบบต่างๆของ 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> 


ผลรัน

---------------------------------------------------------------

การออกรายงานรูปแบบไฟล์เอกสาร Word, Excel
       การออกรายงานในรูปแบบกราฟนั้นจะยกตัวอย่างการใช้ 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
ถ้ารันได้แบบนี้ก็เรียบร้อย


โดยการ Set กราฟนั้นจะมีปัญหาอยู่บ้างเช่น ขึ้น Error ในหน้ารัน


สาเหตุเกิดจากการที่เราไม่ได้ไปตั้งค่าในหน้า 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;
}
?>

ผลรันเมื่อรันถูกต้อง 

บทนำ
Cascading Style Sheets
การใช้ CSS แบบ External
การใช้ CSS แบบ Inline
DBMS ที่ใช้ในการจัดการฐานข้อมูลสำหรับเว็บไซต์
วิธีการแก้ปัญหา z-index ใน IE7 ด้วย jQuery
การเซต timeout ในฟังก์ชัน file_get_contents ของ PHP
วิธีใช้ function Date ของ php หาเวลาย้อนหลัง
การดึงข้อมูลเฉพาะส่วนของ website อื่น ๆ ผ่าน iframe
Refresh Website , iFrame ด้วย Javascipt
วิธีการหา IPAddress ด้วย JavaScript
วิธีการจัดหมวดหมู่ให้เป็นลักษณะรากต้นไม้
ใ้ช้ jQuery ตรวจสอบ ต้องกรอกตัวเลขเท่านั้น
แก้ไขปัญหา ?fb_xd_fragment=
แปลง tag php ให้เป็น html
วิธีส่งเมล์ผ่าน server gmail
cURL VS file_get_contents
ข้อแตกต่างของ ประกาศตัวแปร และ define
function แปลง tis620 เป็น utf8
วิธีการ Query เพิ่ม ลด วันเวลา