รบกวนสอบถามด้วยครับ ผมดึงข้้อมูลจาก mysql แล้วจะให้ออกเป็น PDF โดยทำตามที่พี่วิน POST เอาไว้ ปรากฏว่าไม่ได้ครับ ไม่รู้ว่าผิดตรงไหน ช่วยด้วยนะครับ
รบกวนสอบถามด้วยครับ ผมดึงข้้อมูลจาก mysql แล้วจะให้ออกเป็น PDF โดยทำตามที่พี่วิน POST เอาไว้ ปรากฏว่าไม่ได้ครับ ไม่รู้ว่าผิดตรงไหน ช่วยด้วยนะครับ
Code (code ครับ)
<html>
<head>
<title>ThaiCreate.Com PHP PDF</title>
</head>
<body>
<?php
require('fpdf.php');
class PDF extends FPDF
{
//Load data
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 ***//
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("parttime");
$strSQL = "SELECT * FROM parttime WHERE gender='ชาย'";
$objQuery = mysql_query($strSQL);
$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('logo.png',80,8,33);
$pdf->Ln(35);
$pdf->FancyTable($header,$resultData);
$pdf->Output("MyPDF/MyPDF.pdf","F");
?>
PDF Created Click <a href="MyPDF/MyPDF.pdf">here</a> to Download
</body>
</html>
Tag : PHP, JavaScript
Date :
2012-12-14 16:35:48
By :
sranuwat
View :
904
Reply :
6
ผลที่ได้เป็นแบบนี้ครับ มีแต่หัว
Date :
2012-12-14 16:40:26
By :
sranuwat
ผมเคยลองทำนะครับ เดี๋ยวจะหาที่ผมทำมาให้ดูครับ ดึงได แต่ติดอย่างเดียวที่มันไม่เว้นบรรทัดอัตโนมัตน่ะครับ
Date :
2012-12-14 16:53:58
By :
creativegu
ได้แล้วครับ แต่ว่าที่หน้าที่ 2 หัวแถวไม่โชว์ครับ
Date :
2012-12-14 18:31:22
By :
sranuwat
ไม่มีหัวคอลัมน์แบบนี้ครับ
Date :
2012-12-14 18:33:50
By :
sranuwat
นี่ครับโค้ด ผมรู้สึกว่าตอนนั้นผมใช้ตัวฟอร์มที่ 2 น่ัะครับ ยังใงลองเอาไปปรับแก้เอานะครับ
Code (PHP)
<?php
require('fpdf.php');
class PDF extends FPDF
{
//Load data
function LoadData($file)
{
//Read file lines
$lines=file($file);
$data=array();
foreach($lines as $line)
$data[]=explode(';',chop($line));
return $data;
}
//Better table
function ImprovedTable($header,$data)
{
//Column widths
$w=array(90,90);
//Header
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'L');
//$this->Cell($w[$i],7,iconv('UTF-8', 'TIS-620', $header[$i]),1,0,'C');
$this->Ln();
//Data
foreach ($data as $eachResult)
{
$this->Cell(90,6,$eachResult["member_id"],1,0);
$this->Cell(90,6,$eachResult["status"],1,3);
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,0,'C');
$this->Ln();
}
{
$this->Cell(180,6,'Member CV by Sayhires.Com',0,1);
$this->Cell(180,6,$eachResult[""],0);
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','ชื่อต้น | Title :'),0,0,'L');
$this->Cell(120,6,$eachResult["title_name"],0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,0,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','ชื่อ :'),0,0,'L');
$this->Cell(120,6,$eachResult["name"],0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,0,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','นามสกุล :'),0,0,'L');
$this->Cell(120,6,$eachResult["surname"],0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,0,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','ชื่อ ภาษาอังกฤษ| Name :'),0,0,'L');
$this->Cell(120,6,$eachResult["ename"],0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,0,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','นามสกุล ภาษาอังกฤษ| Surname :'),0,0,'L');
$this->Cell(120,6,$eachResult["esurname"],0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,0,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','เพศ| Sex :'),0,0,'L');
$this->Cell(120,6,$eachResult["sex"],0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,0,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','วัน / เดือน / ปี เกิด| ฺBirth date :'),0,0,'L');
$this->Cell(5,6,$eachResult["date"],0,0,'L');
$this->Cell(20,6,$eachResult["month"],0,0,'L');
$this->Cell(10,6,$eachResult["year"],0,0,'R');
$this->Cell(40,6,iconv( 'UTF-8','TIS-620','อายุ | ฺAge :'),0,0,'R');
$this->Cell(5,6,$eachResult["age"],0,0,'L');
$this->Cell(10,6,iconv( 'UTF-8','TIS-620','ปี | ฺYears old '),0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,0,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','สถานที่เกิด | Birth place:'),0,0,'L');
$this->Cell(120,6,$eachResult["pbirth"],0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,3,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','เชื้อชาติ| Nationality :'),0,0,'L');
$this->Cell(25,6,$eachResult["nation"],0,0,'L');
$this->Cell(40,6,iconv( 'UTF-8','TIS-620','ศาสนา | Religion :'),0,0,'R');
$this->Cell(15,6,$eachResult["religion"],0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,3,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','ส่วนสูง | Height :'),0,0,'L');
$this->Cell(15,6,$eachResult["height"],0,0,'L');
$this->Cell(10,6,iconv( 'UTF-8','TIS-620','ซม | cm'),0,0,'L');
$this->Cell(40,6,iconv( 'UTF-8','TIS-620','น้ำหนัก| Weight :'),0,0,'R');
$this->Cell(15,6,$eachResult["weight"],0,0,'L');
$this->Cell(10,6,iconv( 'UTF-8','TIS-620','กก | KG'),0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,3,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','หมายเลขบัตรประชาชน | ID no:'),0,0,'L');
$this->Cell(120,6,$eachResult["id_card"],0,3,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,3,'C');
$this->Ln();
}
{
$this->Cell(180,6,iconv( 'UTF-8','TIS-620','ข้อมูลติดต่อ | Contact detail'),0,0,'L');
$this->Cell(180,6,$eachResult[""],0,1);
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','ที่อยู่ปัจจุบัน | Address :'),0,0,'L');
$this->Cell(40,6,$eachResult["paddress"],0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,3,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620',''),0,0,'L');
$this->Cell(15,6,$eachResult["amper"],0,0,'L');
$this->Cell(15,6,$eachResult["province"],0,0,'L');
$this->Cell(15,6,$eachResult["country"],0,0,'L');
$this->Cell(15,6,$eachResult["zipcode"],0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,3,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','โทรศัพท์บ้าน | House phone'),0,0,'L');
$this->Cell(15,6,$eachResult["hphone"],0,0,'L');
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','โทรศัพท์มือถือ| Mobile phone'),0,0,'R');
$this->Cell(15,6,$eachResult["phone"],0,0,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,3,'C');
$this->Ln();
}
{
$this->Cell(60,6,iconv( 'UTF-8','TIS-620','อีเมล์ติดต่อ| Email'),0,0,'L');
$this->Cell(120,6,$eachResult["email"],0,3,'L');
//$this->Cell(25,6,number_format($eachResult["day_cus"],2),1,3,'C');
$this->Ln();
}
//Closure line
$this->Cell(array_sum($w),0,'','T');
}
}
$pdf=new PDF();
//Column titles
$header=array('Member_id','Status');
//Data loading
//*** Load MySQL Data ***//
$objConnect = mysql_connect("localhost","root","xxx") or die("Error Connect to Database");
$objDB = mysql_select_db("DB_name");
//mysql_query("set NAMES'UTF8'");// set ÍÑ¡¢ÃÐãËéà»ç¹ Utf8 à¾ÔèÁµÃ§¹ÕéàŤÃѺ ÃѺÃͧä´éªÑǤÃѺ
mysql_query("SET NAMES 'tis620' ");
$strSQL = "SELECT * FROM table WHERE id = '".$_GET["id"]."'";
$objQuery = mysql_query($strSQL);
$resultData = array();
for ($i=0;$i<mysql_num_rows($objQuery);$i++) {
$result = mysql_fetch_array($objQuery);
array_push($resultData,$result);
}
//************************//
//$pdf->AddFont('angsana','','angsa.php');
//$pdf->AddFont('angsana','B','angsab.php');
//$pdf->AddFont('angsana','I','angsai.php');
//$pdf->AddFont('angsana','BI','angsaz.php');
define('FPDF_FONTPATH','font/');
$pdf->AddFont('angsa','','angsa.php');
$pdf->AddFont('angsa','B','angsab.php');
$pdf->AddFont('angsa','I','angsai.php');
$pdf->AddFont('angsa','BI','angsaz.php');
$pdf->SetFont('angsa','',16);
//*** Table 2 ***//
$pdf->AddPage();
$pdf->Image('logo.png',80,8,33);
$pdf->Ln(35);
$pdf->ImprovedTable($header,$resultData);
$pdf->Output("MyPDF/MyPDF.pdf","F");
?>
<CENTER><TD>Report PDF. <a href="MyPDF/MyPDF.pdf" target='_parent'>Click here</a> to Download </TD></CENTER>
</body>
</html>
Date :
2012-12-15 07:11:17
By :
creativegu
ขอบคุณ คุณ creativegu นะครับ แต่กรณีขึ้นหน้าสองยังไม่ได้เลยคร้บ
Date :
2012-12-15 10:33:36
By :
sranuwat
Load balance : Server 03