|
|
Code (PHP)
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=tis-620">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
require('fpdf.php');
define('FPDF_FONTPATH','font/');
class PDF extends FPDF
{
function conv($string) {
return iconv('UTF-8', 'TIS-620', $string);
}
function LoadData($file)
{
//Read file lines
$lines=file($file);
$data=array();
foreach($lines as $line)
$data[]=explode(';',chop($line));
return $data;
}
//Simple table
function BasicTable($header,$data)
{
//Header
$w=array(30,30,55,25,20,20);
//Header
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C');
$this->Ln();
//Data
foreach ($data as $eachResult)
{
$this->Cell(30,6,$eachResult["CustomerID"],1);
$this->Cell(30,6,$eachResult["Name"],1);
$this->Cell(55,6,$eachResult["Email"],1);
$this->Cell(25,6,$eachResult["CountryCode"],1,0,'C');
$this->Cell(20,6,$eachResult["Budget"],1);
$this->Cell(20,6,$eachResult["Budget"],1);
$this->Ln();
}
}
//Better table
function ImprovedTable($header,$data)
{
//Column widths
$w=array(20,30,55,25,25,25);
//Header
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C');
$this->Ln();
//Data
foreach ($data as $eachResult)
{
$this->Cell(20,6,$eachResult["CustomerID"],1);
$this->Cell(30,6,$eachResult["Name"],1);
$this->Cell(55,6,$eachResult["Email"],1);
$this->Cell(25,6,$eachResult["CountryCode"],1,0,'C');
$this->Cell(25,6,number_format($eachResult["Budget"],2),1,0,'R');
$this->Cell(25,6,number_format($eachResult["Budget"],2),1,0,'R');
$this->Ln();
}
//Closure line
$this->Cell(array_sum($w),0,'','T');
}
//Colored table
function FancyTable($header,$data)
{
//Colors, line width and bold font
$this->SetFillColor(255,0,0);
$this->SetTextColor(255);
$this->SetDrawColor(128,0,0);
$this->SetLineWidth(.3);
$this->SetFont('','B');
//Header
$w=array(20,30,55,25,25,25);
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C',true);
$this->Ln();
//Color and font restoration
$this->SetFillColor(224,235,255);
$this->SetTextColor(0);
$this->SetFont('');
//Data
$fill=false;
foreach($data as $row)
{
$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
$this->Cell($w[2],6,$row[2],'LR',0,'L',$fill);
$this->Cell($w[3],6,$row[3],'LR',0,'C',$fill);
$this->Cell($w[4],6,number_format($row[4]),'LR',0,'R',$fill);
$this->Cell($w[5],6,number_format($row[5]),'LR',0,'R',$fill);
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($w),0,'','T');
}
}
$pdf=new PDF();
//Column titles
$header=array('CustomerID','Name','Email','Country Code','Budget','Used');
//Data loading
//*** Load MySQL Data ***//
$id1 = $_GET["CustomerID"];
//echo $id1;
$objConnect = mysql_connect("localhost","root","taweesak") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT c.CustomerID,c.Name,c.Email,c.CountryCode,c.Budget,a.Used FROM customer c inner join audit a on c.customerid=a.customerid where c.customerid='$id1'";
$objQuery = mysql_query($strSQL);
mysql_query("set NAMES'UTF8'");
mysql_query("SET NAMES 'tis-620' ");
$resultData = array();
for ($i=0;$i<mysql_num_rows($objQuery);$i++) {
$result = mysql_fetch_array($objQuery);
array_push($resultData,$result);
}
$pdf->SetFont('Arial','',10);
//*** Table 3 ***//
$pdf->AddPage();
$pdf->Image('one.jpg',10,8,33);
$pdf->Ln();
$pdf->Cell( 0 , 5 , iconv( 'UTF-8','tis-620' , 'WARRANTY CARD' ) , 50 , 1 , 'C' );
$pdf->Ln();
$pdf->Cell( 0 , 5 , iconv( 'UTF-8','cp874' , 'บัตรรับประกันสินค้า' ) , 50 , 1 , 'C' );
$pdf->Ln(35);
$pdf->FancyTable($header,$resultData);
$pdf->Output("MyPDF/MyPDF.pdf","F");
echo "<meta http-equiv=refresh content=0;URL=MyPDF/MyPDF.pdf?>";
?>
</body>
</html>
Tag : PHP, MySQL
|
ประวัติการแก้ไข 2011-08-18 14:51:18
|
|
|
|
|
Date :
2011-08-18 14:50:12 |
By :
e_tawee |
View :
1222 |
Reply :
4 |
|
|
|
|
|
|
|
|
|